是否足够
session_start(); // Must start a session before destroying it
if (isset($_SESSION))
{
unset($_SESSION);
session_unset();
session_destroy();
}
当用户从菜单中选择 Log out
,但没有退出浏览器时,我想彻底删除会话和 $_SESSION
的所有存在。
是否足够
session_start(); // Must start a session before destroying it
if (isset($_SESSION))
{
unset($_SESSION);
session_unset();
session_destroy();
}
当用户从菜单中选择 Log out
,但没有退出浏览器时,我想彻底删除会话和 $_SESSION
的所有存在。
手册链接中有一个完整的工作示例,从那里窃取:为了彻底终止会话,例如注销用户,还必须取消会话ID。如果使用cookie传播会话ID(默认行为),则必须删除会话cookie。可以使用setcookie()进行设置。
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
?>
session_unset
而是使用$_SESSION = array();
? - alexw注意:仅在使用 $_SESSION 的旧版本弃用代码中使用 session_unset()。
- Keno
session_destroy()
前,我不会执行unset($_SESSION);
,因为这可能会导致session_destroy()
无法正常工作。 - Pekka