php5.6 无法获取会话变量,然后删除该变量 [英] php5.6 cannot fetch a session variable and THEN delete the variable
问题描述
今天我有点奇怪.
我发现显然我不能使用会话变量然后销毁它(而不破坏整个会话.)
让我解释一下.如果我这样做,例如:
<预><代码>如果(isset($_SESSION['var'])){$var = $_SESSION['var'];未设置($_SESSION['var']);}var_dump($_SESSION);echo "var: $var";结果是 $var 和键都是 $_SESSION 的 "var" 都是空的.
但是,如果您注释掉取消设置的部分,则 var 将同时出现在 $var 和 $_SESSION 数组中.
即使是更狂热的人:
if (isset($_SESSION['var'])) {$var = array_splice($_SESSION, array_search('var', array_keys($_SESSION)), 1);}
似乎有完全相同的问题.
这太奇怪了.
当我遇到这种糟糕的行为时,我该怎么办?
提前致谢.
我只是做了明显的明智之举:
$session_copy = $_SESSION;如果(isset($session_copy['var'])){$var = $session_copy['var'];未设置($_SESSION['var']);}
是的,也不起作用.
这在 php 文档中得到了回答:https://www.php.net/manual/en/reserved.variables.session.php#85448
<块引用>请注意,如果您将 register_globals 设置为 On,全局变量与 $_SESSION 变量关联的是引用,因此这可能会导致一些奇怪的情况.
这是一个奇怪的情况,但并非没有记录.如果您想更多地了解 php 为何以这种方式处理会话变量,我相信您可以进一步挖掘.
Today I have a bit of an odd one.
I have found out that apparently I cannot use a session variable AND THEN destroy it (without destroying the entire session.)
Let me explain. If I do this, for example:
if (isset($_SESSION['var'])) {
$var = $_SESSION['var'];
unset($_SESSION['var']);
}
var_dump($_SESSION);
echo "var: $var";
The result is "var" is empty both as $var and as a key is $_SESSION.
However, if you comment out the unsetting part, the var is in both $var as well as in the $_SESSION array.
even the much fancier:
if (isset($_SESSION['var'])) {
$var = array_splice($_SESSION, array_search('var', array_keys($_SESSION)), 1);
}
seems to have the exact same problem.
This is sooooo odd.
What can I even do when presented with such a weeeeiiird behavior?
Thank you in advance.
EDIT: I just did the obvious wise-ass thing to do:
$session_copy = $_SESSION;
if (isset($session_copy['var'])) {
$var = $session_copy['var'];
unset($_SESSION['var']);
}
Yep, doesn't work either.
This is answered in the php docs here: https://www.php.net/manual/en/reserved.variables.session.php#85448
Please note that if you have register_globals to On, global variables associated to $_SESSION variables are references, so this may lead to some weird situations.
It is a weird situation, but not undocumented. I'm sure you can dig further if you want to know more about why php handles session variables this way.
这篇关于php5.6 无法获取会话变量,然后删除该变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!