如何从php会话注销 - session_destroy似乎不够

时间:2014-01-05 22:12:03

标签: session cookies php

我正在尝试学习PHP并使用会话。我看到了关于在logout上使用session_destroy的例子,但我在php文档中看到了这一点:

  

为了完全终止会话,要将用户注销,还必须取消设置会话ID。如果使用cookie来传播会话ID(默认行为),则必须删除会话cookie。 setcookie()可以用于此。

那么登出时需要做什么?

6 个答案:

答案 0 :(得分:4)

就像在你引用的段落下方回答你的问题一样的例子: http://php.net/manual/en/function.session-destroy.php

  

示例#1使用$ _SESSION

销毁会话
<?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();
?>

答案 1 :(得分:0)

在我看来,这是必要的

session_start();
unset($_SESSION["nome"]);  // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();**

答案 2 :(得分:0)

我从未真正从会话中删除会话ID。我通常只是从会话中取消设置必要的变量(例如,如果您将登录用户设置为$_SESSION['user'] = $userModel;,那么我只是unset($_SESSION['user']);)。要从用户的浏览器中删除cookie(如文档所述),请执行以下操作:

setcookie(session_id(), "", time() - 3600);

请阅读session_destroy()文档中的Example #1以获取更多信息。

答案 3 :(得分:0)

首先:使用unset()函数。所以,如果你有一个$ _SESSION ['user_id'],你想破坏它:unset($ _ SESSION ['user_id'])

您也可以将unset()用于cookie或其他变种

第二:我们可以有一些代码吗?

答案 4 :(得分:0)

如果您正在使用会话ID。然后你可以这样做

session_start();
unset($_SESSION['id']);
session_destroy();

对于Cookie,您可以将Cookie时间设置为某个过去的日期。

答案 5 :(得分:0)

  

仅对不使用的旧版弃用代码使用session_unset()   $ _SESSION。

在session_start();

session_unset($ _ SESSION [&#39;电子邮件用户&#39;]); session_destroy();`