session variable (kind of) not loading

I'm using session variables to keep track of login information. Specifically whether a user is logged in (boolean $_SESSION["loggedin"]) and their username (string $_SESSION["username"]).

The Homepage has a navigation bar that displays a different set of links depending on whether the user is logged in, by referring to $_SESSION["loggedin"]. This works under all circumstances, except for when the user is redirected to the homepage after login authentication. It doesn't matter if I reload the homepage, it still displays the wrong set of links. ..however if then go to the login page (which redirects to the homepage when $_SESSION["loggedin"] == true), when it sends me back to the homepage, the navigation is displaying correctly. All my pages start with

Here is the redirect from login.php after a successful login:

    elseif ($phash === crypt($pword,$phash)){
    $_SESSION["loggedin"]=true;
    $_SESSION["username"]=$username;
    header("Location: index.php");
    die();
    }

Here is the redirect code in login.php for when the user is already logged in:

    <?php
    session_start();

    if ($_SESSION["loggedin"] == true){
      header("Location: index.php");
      die();
    }

Here is the display code:

    <?php if ($_SESSION["loggedin"]==true){
    echo "<li><a href='logout.php'> Logout </a></li>";
    //...several similar echoes...
    }
    else{
    echo "<li><a href='login.php'> Login </a></li>";
    //...several similar echoes...
    }

I'm baffled as to why that little piece of redirect code in the beginning of my login.php would suddenly cause the navigation to display correctly when it gets back to index.php, and I'm feeling a bit out of my league.

..and as it turns out, if I just let the index.php page sit for 30 seconds or so, then refresh, it will display correctly. I added a var_dump() to the page, and when index.php is displaying incorrectly, the session variable is NULL. When I come back and refresh a little while later, the session variable is true.

链接地址: http://www.djcxy.com/p/59736.html

上一篇: while循环的不同值

下一篇: 会话变量(种类)不加载