php - 未删除的 Cookie

标签 php cookies

奇怪的问题。

这是 index.php:

session_start();
print_r($_COOKIE);
print_r($_SESSION);

这是 logout.php:

session_destroy();
$_COOKIE['key'] = "";
$_COOKIE['usr_email'] = "";
setcookie("key", "", time() - 3600);
setcookie("usr_email", "", time() - 3600);
header("Location: http://www.site.net/index.php");

当我加载 logout.php 时,在被重定向到 index.php 之后我得到:

Array
(
    [fc] => fcVal=6927578914025605120
    [PHPSESSID] => na015ipu3s69hhj00sgd0h1es6
)
Array
(
    [key] => cc2bffe0c1e36bc5790f5b78b11e5f50
    [usr_email] => myemail@gmail.com
)

session 正常,因为我在加载 index.php 时生成它们,但是 cookie 怎么仍然存在?

注意

我在 index.php 上还有一些代码,用于确定登录表单是否已填写并开始对用户进行身份验证,如果用户/密码正确,它会设置两个 session 变量和两个 cookie 变量(usr_email 和 key )。

此刻,我在登录表单之后,所以我有 cookie 和 session 变量,并且由于 cookie 没有被删除,我禁用了索引页面上的所有其他 PHP 代码,因为我认为可能是什么东西自动让我登录。所以目前我已经通过身份验证,然后从索引页面中注释掉了上面没有提到的其余部分。

也相关 这是我在用户通过身份验证时设置 cookie 的方式:

    setcookie("key", $cookie['key'], time() + 36000);
    setcookie("usr_email", $cookie['usr_email'], time() + 36000);

PHP 版本:PHP 5.3.6-13

上次编辑

正如 Josh 在评论中所写,我混淆了 cookie 打印和 session ,我无法删除 session 变量而不是 cookie 变量。

最佳答案

来自PHP Manual :

... [session_destroy()] does not unset any of the global variables associated with the session, or unset the session cookie...

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

注意:PHPSESSID 是传播 session ID 的 cookie。

尝试以下操作以清除您的 session 变量以防止重复使用:

// Example #1 Destroying a session with $_SESSION
// 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_start() 必须在 session_destroy() 工作之前调用。

关于php - 未删除的 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9166002/

相关文章:

php - 防止引导模式在表单提交后关闭

PHP - mySQL "like"查询不返回任何内容

node.js - 如何将 cookie 从 apollo-server 传递到 apollo-clenet

c# - 创建 Cookie ASP.NET 和 MVC

python请求启用cookies/javascript

angularjs - Browser.manage().addCookie() 返回错误丢失 'name'

javascript - 我如何在 adonisJS 中翻译从 php 编写的这段代码?

php - 如何使用 php 中的 session 变量将我的数据库添加到 html 表中

javascript - CKEditor XSS 清理

python - 传入 cookie python 请求