1

我正在设置一个身份验证cookie,如下所示:

$identifier = $this->createIdentifier($username);
$key = md5(uniqid(rand(), true));
$timeout = time() + 60 * 60 * 24 * 100;

setcookie('auth', "$identifier:$key", $timeout);

注销后,我试图通过这样做使其无效:

setcookie('auth', "", time() - 3600);

当我在注销后尝试查看受限页面时,我正在检查 cookie 是否存在:

if (isset($_COOKIE['auth'])) {
error_log("COOKIE EXISTS: " . print_r($_COOKIE, true));
}

这是我的注销脚本:

if (!isset($_SESSION)) session_start();

$ref="index.php";

if (isset($_SESSION['username'])) {
unset($_SESSION['username']);   

session_unset();
session_destroy();

// remove the auth cookie
setcookie('auth', "", time() - 3600);

}

header("Location: " . $ref);
exit();

我不应该点击这个代码,但我是。注销后,我看到 cookie 已从浏览器中删除。知道注销后如何再次找到它吗?

更新 此代码从另一个检查用户权限等的类中调用。唯一不能使用的文件是从上面一个目录引用它的文件。例如

任何像这样引用它的文件都可以正常工作:

<?php include_once('classes/check.class.php'); 

任何引用它的文件都不能正常工作:

<?php include_once('../classes/check.class.php'); 

有什么想法可能导致这种情况吗?

4

1 回答 1

2

注销用户后,您需要进行重定向以导致新页面加载。由于 cookie 是与页面请求一起发送的,直到发出新请求,这些 cookie 即使在您“删除”它们之后仍然存在。

于 2013-01-21T15:22:38.313 回答