销毁整个会话

如果你有一个你想破坏的会话,你可以用 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()