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?
Using your example, I created this test:
On the first load, it outputs:
On reload:
On second reload:
So it appears your code is resetting the cookie on the roundtrip.
EDIT
The following:
Will either print:
Or will print:
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, useunset()
.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.
Or to allow for paths contained within a subdirectory...
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.
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
How about setting the time to an hour back, rather than that large number?