PHP会话变量isset(..)=1在session_start()之后

发布于 2024-10-11 10:50:36 字数 769 浏览 8 评论 0原文

我想我不理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九八野马 2024-10-18 10:50:36

代码有几个问题:

  1. 测试 !session_id()=="" 是错误的,因为 ! 的优先级高于 ==.它应该写成 session_id() != ""。由于隐式转换,它应该可以正常工作,但我不能说这只是一个错误。
  2. session_destroy 仅在 session_start 之后调用才有效。

现在,具体发生的情况如下:

  1. 您测试活动会话。由于没有,测试失败并且永远不会调用 session_destroy (即使调用它也不会执行任何操作)。
  2. 您测试 $_SESSION["first_date_of_week"] 是否存在。由于没有活动会话,isset 返回 false。
  3. 您开始会议。 $_SESSION["first_date_of_week"] 现在可用,但这只是因为您在某个较早的会话中以未显示的代码设置了它

尝试以下代码以更好地处理正在发生的事情:

$logout = true; // play with this
session_start();
if ($logout) {
    session_destroy();
    echo "Logged out.";
    die;
}

if (!isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 1;
}
else {
    ++$_SESSION['counter'];
}

echo "The counter value is ".$_SESSION['counter'];
die;

There are a couple of problems with the code:

  1. The test !session_id()=="" is wrong because ! has a higher precedence than ==. It should be written as session_id() != "". Due to implicit conversions it should work correctly, but I can't say it's anything else than a bug.
  2. session_destroy only works if called after session_start.

Now, here's what happens exactly:

  1. You test for an active session. Since there's none, the test fails and session_destroy is never called (it would do nothing even if it were called).
  2. You test for the existence of $_SESSION["first_date_of_week"]. Since there's no active session, isset return false.
  3. You start the session. $_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:

$logout = true; // play with this
session_start();
if ($logout) {
    session_destroy();
    echo "Logged out.";
    die;
}

if (!isset($_SESSION['counter'])) {
    $_SESSION['counter'] = 1;
}
else {
    ++$_SESSION['counter'];
}

echo "The counter value is ".$_SESSION['counter'];
die;
话少情深 2024-10-18 10:50:36

请记住,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 :)

深海夜未眠 2024-10-18 10:50:36
session_start();
if(isset(session_id())) {
    session_destroy();
}
$_SESSION['data'] = "something";
session_start();
if(isset(session_id())) {
    session_destroy();
}
$_SESSION['data'] = "something";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文