PHP 销毁整个会话

示例

如果您有一个想要取消的会话,则可以使用 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();

// 如果需要终止会话,请同时删除会话cookie。
// 注意:这将破坏会话,而不仅仅是会话数据!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

//最后,我们可以销毁会话:
session_destroy();

使用session_destroy()与使用类似的东西不同,后者会删除存储在超全局变量中的所有值,但不会破坏会话的实际存储版本。$_SESSION = array();SESSION


注意:我们使用而不是因为手册规定:$_SESSION = array();session_unset()

仅session_unset()用于不使用$_SESSION的旧版本代码。