銷燬整個會話
如果你有一個你想破壞的會話,你可以用 session_destroy()
做到這一點
/*
Let us assume that our session looks like this:
Array([firstname] => Jon, [id] => 123)
We first need to start our session:
*/
session_start();
/*
We can now remove all the values from the `SESSION` superglobal:
If you omitted this step all of the global variables stored in the
superglobal would still exist even though the session had been destroyed.
*/
$_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 we can destroy the session:
session_destroy();
使用 session_destroy()
與使用 $_SESSION = array();
之類的東西不同,$_SESSION = array();
會刪除儲存在 SESSION
超全域性中的所有值,但不會破壞實際儲存的會話版本。
注意 :我們使用 $_SESSION = array();
而不是 session_unset()
,因為手冊規定:
僅對不使用$ _SESSION 的舊版棄用程式碼使用
session_unset()
。