0

所以我从 php 开始,我有一个非常简单的设置。我有 2 页。一个页面是登录页面,另一个是登录页面。我现在的问题是每当我成功登录时,第二页仍然认为我没有登录。这是我的代码:

我知道我登录正确,因为它没有打印“错误”,并且在进入下一页之前我看到了“到这里”打印。登录页面:

<!DOCTYPE html>
<html>
    <head>
        <?php 
        session_start();
        ?>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <?php 
        if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
            ?>
            <script language="Javascript"> window.location.href="show.php";</script>
            <?php
        }?>
    </head>
    <body>
            <form method="post">
                Username: <input type="text" name="username" size="15" /><br />
                Password: <input type="password" name="pass" size="15" /><br />
                <input type="submit" name ="submit1" value="Login"/>
            </form>
        <?php 
            if (isset($_POST['submit1'])) {
                if (LOGGEDIN) {
                    print "GOT HERE";
                    $_SESSION['loggedin'] = true;
                    $_SESSION['user'] = $username;
                    ?>
                    <script language="Javascript"> window.location.href="show.php";</script>
                    <?php
                }
                else {
                    print "WRONG!";
                }
            }

        ?>
    </body>
</html>

登录页面:

<!DOCTYPE html>
<html>
    <head>
        <?php session_start();?>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <?php 
        if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
            ?>
            <script language="Javascript"> window.location.href="index.php";</script>
            <?php
        }?>
    </head>
    <body>WELCOME
    </body>
</html>
4

1 回答 1

5

你不应该把你的放在session_start()任何其他代码之前吗?

<?php session_start(); ?>
<!DOCTYPE html>
<html>
...
于 2013-06-14T01:16:49.440 回答