简体   繁体   中英

Internet Explorer doesn't unset cookie even with php code

I got a problem with Internet Explorer that I really don't understand and as you guess, it's with IE in it's last version.

The situation is very simple. I set a cookie, display it, then unset it then display "no cookie".

With three simple page :

setcookie.php

<?php
    setcookie('test','test value 2', (time()+60*60*24*30), '/', '.mydomain.com');
?>

unsetcookie.php

<?php
    unset($_COOKIE['test']);
    setcookie('test','unset value', (time() - 3600 ), '/', '.mydomain.com');
?>

test.php

<?php
if(isset($_COOKIE['test'])){
    $test = 'cookie value ==> '.$_COOKIE['test'];
}else{
    $test = 'no $$$$$$ cookie';
}

echo $test;
?>

So the simple procedure is :

1/ open setcookie.php

2/ open test.php (all browsers displays "cookie value ==> test value 2")

3/ open unsetcookie.php

4/ open test.php again

Then Chrome, Safari etc... displays "no $$$$$ cookie" BUT IE displays "cookie value ===> unset value"

And I want him to display like the others one... I tried to set null as value, I tried to set the time at 0, -1. I tried tu unset after the set cookie etc... doesn't change anything...

So I'm a bit desperate now and I'm very frustrated that the cookie code is server side so how is that possible that IE misread this stuff ???

I know I could manage to check the cookie value to check if it was unset but that's not how I made my code and it will imply a lot of stupid change...

Thanks for your help !

Technically speaking, removing unused cookies is left to the browser to deal with. Normally, when a cookie is past it's expire time, or has a null value, it gets removed automatically by the browser, not the server. That being said, instead of:

setcookie('test','unset value', (time() - 3600 ), '/', '.mydomain.com');

You should have this:

setcookie('test',null , (time() - 36000 ), '/', '.mydomain.com');

In which the time at which the cookie expired is significantly more exaggerated, and the value is null. In all honesty, since the server doesn't decide what cookies to keep, and which to remove (rather to suggest that the browser SHOULD remove the cookie), you should do value checking rather than checking to see if the cookie is set. When you want to remove a cookie, set its value to something that you can easily recognize. Here's an example:

Logout:

//I want to remove the cookie
setcookie('test','idontwantthisanymore',(time() - 3600), '/', '.mydomain.com);

Page to test login status:

if(!isset($_COOKIE['test']||($_COOKIE['test']=="idontwantthisanymore")) {
echo 'no cookie';
} else {
echo 'cookie';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM