问题描述
session_start();
$_SESSION['user'] = "789456";
$_SESSION['name'] = "dummy";
$_SESSION['id'] = "123";
print_r($_SESSION);
session_destroy();
echo "Session End";
print_r($_SESSION);
我的输出是:
Array ( [user] => 789456 [name] => dummy [id] => 123)
Session End :Array ( [user] => 789456 [name] => dummy [id] => 123)
输出不应该只是:
Array ( [user] => 789456 [name] => dummy [id] => 123)
如果我在 session_destroy()
之前使用 session_unset()
那么我会得到我期望的结果.在session_destroy()
之前是否总是需要使用session_unset()
?
If I use session_unset()
before session_destroy()
then I get the result I expect. Is it always necessary to use session_unset()
before session_destroy()
?
推荐答案
来自 文档:
session_destroy() 销毁与当前会话相关的所有数据.它不会取消设置与会话关联的任何全局变量,也不会取消设置会话 cookie.要再次使用会话变量,必须调用 session_start().
为了完全终止会话,比如注销用户,会话 ID 也必须取消设置.如果使用 cookie 来传播会话 ID(默认行为),则必须删除会话 cookie.setcookie() 可以用于此目的.
In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.
最简单的方法是:$_SESSION = array();
在调用 session_destroy();
之后.
The easiest way would be: $_SESSION = array();
after calling session_destroy();
.
这篇关于即使使用 session_destroy() 会话变量也不会被销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!