views:

84

answers:

6

I've tried searching the php manual and internet on how to delete cookies and I've tried it the exact same way they all say:

setcookie("name", '', 1);

or

setcookie("name", '', time()-3600);

But when I check the cookies in the cookies dialog in Firefox, it's still there with the same value. I set this cookie using the following line:

setcookie("name", $value, time() + 259200, $path);

I found this question on stackoverflow: http://stackoverflow.com/questions/2497501/cookie-wont-unset, but none of the answers solved the problem. I also tried putting all paramaters in, like the author said, but it had no effect.

Does anyone see the problem?

A: 

Have you tried setting the time to a small value and using a value for cookie?

setcookie("name", 'n', 1);
Sandeep
I found out that I can actually change the values if the expiration date is in the future. Then the value doesn't matter. But using '1' as time doesn't seem to work, as does time()-3600.
RemiX
A: 

I tried using

setcookie("name", "", -1);

and on my server with Apache/PHP5 it cleared the cookie (at least a var_dump($_COOKIE) showed an empty array).

Keeper
A: 

Did you check if your script already send its HTTP headers?

if (headers_sent()) {
  trigger_error("Cant change cookies", E_USER_NOTICE);
}
Bob Fanger
No header errors.
RemiX
A: 

Happens to me as well one in ten times though. I guess its a problem with the way we code.

This is my code

setcookie("token", "", time() - 36000, "/");
atif089
What exactly is the difference? I also tried using the path parameter, if that's what you mean?
RemiX
No, I mean to say even I get the same problem as you.
atif089
+3  A: 

The manual states:

Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.

So also make sure that $path is specified correctly -- also when deleting it. For instance, if the cookie was specified in a subdirectory, you may not be able to delete it from either the parent or children directories (or both).

I'm not entirely sure how the permissions work, but you might want to use the Web Developer Toolbar to view what the path is of the cookie you are attempting to delete.

Paul Lammertsma
I'm checking the Firefox Cookies dialog, and the path I'm using to set the cookie is the same as the path I'm using to unset it. But the address where I try to unset the cookie is not the same as the path I set the cookie to.I also found out that I can change the value of the cookie, if the expiration date is in the future. If I use a value in the past, nothing happens. Weird?
RemiX
+2  A: 

Ok, I really don't understand, but it works now. The magic code is:

setcookie("name", '', 1, $path);

Haven't I already tried that??! Whatever, it works now. Thanks for your help, people!

RemiX