Toni Michel Caubet Toni Michel Caubet - 4 months ago 23
PHP Question

cookie wont unset

I am unable to get the cookie to unset.

cookie set: (id, alias)

setcookie("id",$data['id'], time()+3600*24*30);
setcookie("alias",$this->nombre, time()+3600*24*30);


cookies unset? (id, alias)

setcookie("id","-1",time()-315360000);
setcookie("alias","",time()-315360000);
unset($_COOKIE['id']); // additional, but still no..
unset($_COOKIE['alias']); // " "


What I am doing wrong?

Answer

Using your example, I created this test:

<?php

ob_start();

echo '<pre>';

setcookie("id","0001", time()+3600*24*30);
setcookie("alias","name", time()+3600*24*30);

print_r($_COOKIE);

if ($_COOKIE['id'] || $_COOKIE['alias']) {
    setcookie("id","-1",time()-315360000);
    setcookie("alias","",time()-315360000);
}

print_r($_COOKIE);

ob_end_flush();

?>

On the first load, it outputs:

Array
(
)
Array
(
)

On reload:

Array
(
    [id] => 0001
    [alias] => name
)
Array
(
    [id] => 0001
    [alias] => name
)

On second reload:

Array
(
)
Array
(
)

So it appears your code is resetting the cookie on the roundtrip.

EDIT

The following:

<?php

ob_start();

echo '<pre>';

setcookie("id","0001", time()+3600*24*30);
setcookie("alias","name", time()+3600*24*30);

print_r($_COOKIE);

if ($_COOKIE['id'] || $_COOKIE['alias']) {
    setcookie("id","-1",time()-315360000);
    setcookie("alias","",time()-315360000);
    unset($_COOKIE['id']);
    unset($_COOKIE['alias']);
}

print_r($_COOKIE);

ob_end_flush();

?>

Will either print:

Array
(
)
Array
(
)

Or will print:

Array
(
    [id] => 0001
    [alias] => name
)
Array
(
)

http://jfcoder.com/test/cookies.php (hit reload a few times)

If you need to tell the browser to forget the cookie, use setcookie() with the time set back in time (I use at least 24 hours). If you need the $_COOKIES array to forget the value, use unset().

EDIT

There are two possible issues contributing here, one a subdomain mismatch on the cookie, and a path accessibility problem.

For instance...

If the url the visitor accessed was on a directory different from where the url that attempts to reset the cookie, you need to set the cookie with a path that will allow that cookie to be accessed (and reset) by other paths.

setcookie('my', 'cookie', time()+3600, '/');

Or to allow for paths contained within a subdirectory...

setcookie('my', 'cookie', time()+3600, '/my/path/');

If the url the visitor accessed was a subdomain (including www), but you want the cookie to be accessible to all subdomains, you need to give a wildcard to setcookie.

setcookie('my', 'cookie', time()+3600, '/', '.example.com');

Will allow urls from www.example.com, my.example.com, and sub.example.com to access and reset the cookie. Obviously, at this point too your path considerations need to be taken into account, since for a subdomain argument, you will need to include a path. / selects all subdirectories on the url, and . before the domain selects subdomains (although sub.sub.domains, I'm not sure).

http://php.net/manual/en/function.setcookie.php

Comments