嵌入式PHP忽略条件语句


Embedded PHP ignoring conditional statements

我正试图有条件地在我的网站右上角创建一个字段集,该字段集将提示用户登录,或者显示他们的用户名并为他们提供注销选项。

为了实现这一点,我在html文档中使用了嵌入式php。不幸的是,这两个字段集都显示在我的网页上。有人能向我解释一下我做错了什么吗?非常感谢。

    <div id="loginForm">
            <?php 
            session_start();

            if(isset($_SESSION['username'])){ ?>
                <form action="endsession.php" method="post">
                <fieldset width="100">
                <legend>Welcome</legend>
                <h3></h3>
                <input type="submit" value="Logout"></input>
                </fieldset>
                </form>
            <?php } else{ ?>
                <form action="connection.php" method="post">
                    <fieldset width="100">
                    <legend>Login</legend>
                    <input type="hidden" name="submitted" id="submitted" value="1"/>
                    <label for="username">Username:</label>
                    <input id="username" name="username" type="text" ><br><br>
                    <label for="password">Password:</label> 
                    <input id="password" name="password" type="password"/><br><br>
                    <input type="submit" value="Sign in"></input>
                    </fieldset>
                    </form>
            <?php } ?>
        </div>

Putsession_start()在任何其他代码之前位于页面顶部;就在PHP开始标记之后

<?php
session_start();
<div id="loginForm">
....
and so on...

希望它能解决问题。