PHP 中的 Cookie 未重置

发布于 2024-12-09 01:12:30 字数 543 浏览 1 评论 0原文

我是饼干新手。我创建了一个用户登录 php 类,它使用 cookie 来存储唯一的 MD5 密钥来记住已登录的用户。但是,当用户注销时,cookie 不会被重置。我根据在堆栈溢出上找到的代码创建了一个函数,用于在注销时清除 cookie。

static public function clearCookies() 
{
    $past = time() - 3600;
    foreach ( $_COOKIE as $key => $value )
    {
            $value = '';
        setcookie( $key, $value, $past );
        setcookie( $key, $value, $past, '/' );
    }
}

然而,cookie 仍未被清除。

这是设置 cookie 的代码行

setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7);

提前致谢

I am new to cookies. I created a user login php class that uses a cookie to store a unique MD5 key to remember a user that has logged in. However, the cookie is not being reset when the user logs out. I created a function from code I found on stack overflow to clear the cookies on logout.

static public function clearCookies() 
{
    $past = time() - 3600;
    foreach ( $_COOKIE as $key => $value )
    {
            $value = '';
        setcookie( $key, $value, $past );
        setcookie( $key, $value, $past, '/' );
    }
}

However, the cookie is still not being cleared.

This is the line of code that sets the cookie

setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7);

Thanks ahead of time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じ违心 2024-12-16 01:12:30

尝试添加

static public function clearCookies() 
{
    $past = time() - 3600;
    foreach ( $_COOKIE as $key => $value )
    {
            $value = '';
        setcookie( $key, $value, $past );
        setcookie( $key, $value, $past, '/' );
        unset($_COOKIE[$key]);
    }
}

您必须注意,更改后的cookie在发送到客户端后是可读的(如果您没有通过$_COOKIE手动设置它们),因此下次刷新。

Try to add

static public function clearCookies() 
{
    $past = time() - 3600;
    foreach ( $_COOKIE as $key => $value )
    {
            $value = '';
        setcookie( $key, $value, $past );
        setcookie( $key, $value, $past, '/' );
        unset($_COOKIE[$key]);
    }
}

You have to note that changed cookies are readable AFTER sending them to client (if you do not set them manually via $_COOKIE), so the next refresh.

横笛休吹塞上声 2024-12-16 01:12:30

这是有效的解决方案。

我更改

setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7);

setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7, '/');

“似乎 cookie 没有被重置”,因为重置它的 url 与设置它的 url 不同。添加“/”后,可以从新 url 重置它。

Here is the solution that worked.

I changed

setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7);

to

setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7, '/');

It seems that the cookie was not being reset because the url it was being reset from was different than the url it was set in. After adding '/' it could be reset from the new url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文