Cookie尚未设置 [英] cookie is not unsetting

查看:96
本文介绍了Cookie尚未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的登录脚本中,如果用户选择记住我,它将设置如下cookie:

In my login script, if a user select 'remember me' it sets cookie like this:

setcookie("a", $valuea, time()+2595000, "/");
setcookie("b", $valueb, time()+2595000, "/");

当用户(带有记住我)选择注销时,logout.php脚本会取消设置Cookie通过以下方式:

and when a user (with 'remember me') select logout, the logout.php script unset cookie by the following way:

if(isset($_COOKIE['a']) && isset($_COOKIE['b'])){
setcookie("a","", time()-2595000, "/");
setcookie("b","", time()-2595000, "/");
setcookie(session_id(),"",time()-2595000, "/");
}

但是,注销后,用户将重定向到登录页面,并且登录页面检查用户登录状态通过以下代码:

However, after logout the user is redirected to login page and login page checks the user login status by the following code:

if($_COOKIE['a']=='' || $_COOKIE['b']==''){
echo 'You are not logged in.'; 
}else{
echo 'You are logged in with remember me.Your cookie is: '.$_COOKIE['a'].' and '.$_COOKIE['b'];
}

但是我发现用户没有注销,并且cookie显示值。
我没有找到为什么setcookie没有清除cookie a和b的值。
有任何想法吗?

But I found that user is not logged out and cookie is showing with value. I am not finding why the setcookie is not clearing the value of cookie a and b. Any idea?

推荐答案

最后,我发现了问题所在。
实际上是在实际代码中,发生了什么,我刚在同一注销页面中删除后检查了$ _COOKIE(而不是在重定向到登录页面之后)。我忘记了cookie是由浏览器发送的,如果您不转到下一页,则cookie的更改对您将不可见。因此,如果您在logout.php中尝试此操作:

Atlast I found the problem. Actually in real code, what was happening, I was checking $_COOKIE just after deletion in the same logout page (not after redirecting to login page). I forgot that the cookie is sent by the browser and if you do not go to next page, changes in cookies will not be visible to you. So, if you try this in logout.php:

//deletion of cookies
if(isset($_COOKIE['a']) && isset($_COOKIE['b'])){
setcookie("a","", time()-2595000, "/");
setcookie("b","", time()-2595000, "/");
setcookie(session_id(),"",time()-2595000, "/");
}

//checking the existence of cookies
if($_COOKIE['a']=='' || $_COOKIE['b']==''){
echo 'You are not logged in.'; 
}else{
echo 'You are logged in with remember me.Your cookie is: '.$_COOKIE['a'].' and '.$_COOKIE['b'];
}

然后它将为您提供虚假信息。尽管删除了cookie,但是您会看到您已登录........,因为在logout.php中由php获取的cookie将保留在php内存,直到用户移至下一页。如果您在接下来的任何页面中检查这些cookie的存在,那么您将看到没有cookie(那些cookie确实已删除。)

Then it will give you false information. Although the cookies are deleted, but you will see "You are logged in ..................." because cookies which was get by php in logout.php will remain in the php memory until user moves to next page. If you check the existence of those cookies in next any page, then you will see there is no cookie (those were really deleted.)

我的问题是所有专家,有什么方法可以交叉检查删除后在同一页面中是否真的删除了Cookie?

My question is to all experts, is there any way to cross-check whether the cookies are really deleted in the same page after deletion?

这篇关于Cookie尚未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆