PHP会话变量isset(..)=1在session_start()之后
我想我不理解 PHP 中会话变量的范围或会话本身,因此这个问题:
这是我的代码
if(session_id()!=""){
echo "Getting rid of session"."</br>";
session_destroy();
}
echo "Before session_start(): ".isset($_SESSION["first_date_of_week"])."</br>";
session_start();
echo "After session_start(): ".isset($_SESSION["first_date_of_week"])." ".$_SESSION["first_date_of_week"]->format("Y-m-d")."</br>";
输出是:
Before session_start():
After session_start(): 1 2011-01-09
在会话变量上执行 isset(..) 时,它是如何设置的启动会话后立即,即使我什至还没有使用它或设置它?然而,它仍然具有与以前相同的价值。
另外,session_id()="" 因为 if 子句永远不会被触发。我从来没有杀死会话,为什么它被设置为“”?即我刷新页面并期望会话仍然有效。
如果已经设置了 isset(..) 函数,那么使用 isset(..) 函数进行测试是非常无用的...
提前致谢!
/尼克拉斯
I guess I am not understanding the scope of session variables, or the session itself, in PHP, hence this question:
This is my code
if(session_id()!=""){
echo "Getting rid of session"."</br>";
session_destroy();
}
echo "Before session_start(): ".isset($_SESSION["first_date_of_week"])."</br>";
session_start();
echo "After session_start(): ".isset($_SESSION["first_date_of_week"])." ".$_SESSION["first_date_of_week"]->format("Y-m-d")."</br>";
The output is:
Before session_start():
After session_start(): 1 2011-01-09
How come that when doing the isset(..) on the session variable it is set directly after starting the session, even though I haven't even used it or set it yet? It does, however, still have the same value as before.
Also, session_id()="" since the if-clause is never triggered. I never kill the session, how come it is set to ""? I.e. I refresh the page and expects the session to still be alive.
Using the isset(..) function is then pretty useless testing if it has been set already...
Thanks in advance!
/Niklas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码有几个问题:
!session_id()==""
是错误的,因为!
的优先级高于==.它应该写成
session_id() != ""
。由于隐式转换,它应该可以正常工作,但我不能说这只是一个错误。session_destroy
仅在session_start
之后调用才有效。现在,具体发生的情况如下:
session_destroy
(即使调用它也不会执行任何操作)。$_SESSION["first_date_of_week"]
是否存在。由于没有活动会话,isset
返回 false。$_SESSION["first_date_of_week"]
现在可用,但这只是因为您在某个较早的会话中以未显示的代码设置了它。尝试以下代码以更好地处理正在发生的事情:
There are a couple of problems with the code:
!session_id()==""
is wrong because!
has a higher precedence than==
. It should be written assession_id() != ""
. Due to implicit conversions it should work correctly, but I can't say it's anything else than a bug.session_destroy
only works if called aftersession_start
.Now, here's what happens exactly:
session_destroy
is never called (it would do nothing even if it were called).$_SESSION["first_date_of_week"]
. Since there's no active session,isset
return false.$_SESSION["first_date_of_week"]
now becomes available, but only because you had set it on some earlier session, in code that you don't show.Try this code to get a better handle on what's going on:
请记住,session_start 不会启动新会话,而只是启动“会话”。因此,如果您设置了某些内容(并且尚未销毁它,请参阅 @Jon 的答案),那么您将恢复会话:)
Remember that session_start doesn't start a new session, just starts "the session". So if you've set something (and you haven't destoryed it, see @Jon 's answer), then you'll get your session back :)