PHP 中的 Cookie 未重置
我是饼干新手。我创建了一个用户登录 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试添加
您必须注意,更改后的cookie在发送到客户端后是可读的(如果您没有通过$_COOKIE手动设置它们),因此下次刷新。
Try to add
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.
这是有效的解决方案。
我更改
为
“似乎 cookie 没有被重置”,因为重置它的 url 与设置它的 url 不同。添加“/”后,可以从新 url 重置它。
Here is the solution that worked.
I changed
to
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.