即使使用 session_destroy() 会话变量也不会被销毁 [英] Session Variables do not get destroy even using session_destroy()

查看:53
本文介绍了即使使用 session_destroy() 会话变量也不会被销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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().

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

为了完全终止会话,比如注销用户,会话 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() 会话变量也不会被销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆