php5.6 cannot fetch a session variable and THEN delete the variable

旧时模样 提交于 2019-12-23 03:28:27

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/59219215/php5-6-cannot-fetch-a-session-variable-and-then-delete-the-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!