简体   繁体   中英

session variable already set

I have a 'loginStatus' session variable that gets set on the first page load. I have an if/else to echo 'setting for the first time' comments to the browser when this 'loginStatus' is being set for the very first time. The 'setting for the first time' NEVER APPEARS! I have cleared the cache, I have tried navigating to the index.php page in a browser window outside of my Netbeans development environment -- doesn't matter. It's as if the session is staying alive permanently on my localhost web server.

Here is the code that detects for the un-initialized 'loginStatus' session variable for the first time the page is loaded and creates/initilizes the session variable just once.

if ( isset($_SESSION['loginStatus']))
{
    // we get here ONLY if the 'loginStatus' session variable has already been
    // created for this user's session.  
    $_SESSION['loginStatus'] = "loginStatus already set!"; 
    echo '<br />Just set the loginStatus to: ' .  $_SESSION['loginStatus'] . '<br /><br />';    
}
else
{
    // we only get here the first time this is sent by the server
    // to the user's browser -- so we need to create the 'loginStatus' session
    // variable because the user just came to our site
    // and has not yet logged in. 
    $_SESSION['loginStatus'] = "First-time initialization of loginStatus"; 
    echo '<br />Just set the loginStatus for the first time!<br /><br />';
}

I never see in the browser window "Just set the loginStatus for the first time!"

All I see in the browser window is: "Just set the loginStatus to: loginStatus already set!"

In other words -- isset() is wrongly returning true the very first time my page loads!
I see NO justification for a session variable that is magically already there when I first load the page in the browser!

Although I have loaded the above page several times tonight while writing the code, it was my understanding that when you leave the final page the session gets destroyed. So why is my $_SESSION['loginStatus'] variable hanging around like a relative who has worn out their welcome?

Session get destroyed when and by the rules set to it (to the session GC) in the php.ini file.This does not happen necessarily when you close the browser or browse to other pages.
Read the session manual in php.net and set the session rules the best way that fits you.

For example, on high security sites, I give the session a time out of few minutes. Means, if the user does no action that makes a request to the server within that time limit, the session will expire.

not sure, but it may because you have save this values in your cookies,

try to clear cookies, and reload the page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM