简体   繁体   English

会话变量已设置

[英]session variable already set

I have a 'loginStatus' session variable that gets set on the first page load. 我有一个'loginStatus'会话变量,该变量在首页加载时设置。 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. 当第一次设置“ loginStatus”时,我有一个if / else向浏览器回显“第一次设置”的注释。 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. 我已经清除了缓存,尝试在Netbeans开发环境之外的浏览器窗口中导航到index.php页面-没关系。 It's as if the session is staying alive permanently on my localhost web server. 好像该会话在我的本地主机Web服务器上永久保持活动状态。

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. 这是代码,该代码在首次加载页面时检测未初始化的“ loginStatus”会话变量,并仅创建/初始化一次会话变量。

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!" 从没有在浏览器窗口中看到“仅第一次设置loginStatus!”。

All I see in the browser window is: "Just set the loginStatus to: loginStatus already set!" 我在浏览器窗口中看到的是:“只需将loginStatus设置为:loginStatus已设置!”

In other words -- isset() is wrongly returning true the very first time my page loads! 换句话说,isset()在我的页面第一次加载时错误地返回了true
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 ['loginStatus']变量像一个已经不受欢迎的亲戚一样徘徊?

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. 会话在php.ini文件中设置的规则(以及对会话GC设置的规则)时被销毁。当您关闭浏览器或浏览到其他页面时,不一定发生这种情况。
Read the session manual in php.net and set the session rules the best way that fits you. 阅读php.net中的会话手册,并设置适合您的最佳会话规则。

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, 不确定,但这可能是因为您已将此值保存在Cookie中,

try to clear cookies, and reload the page 尝试清除Cookie,然后重新加载页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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