[ACCEPTED]-how to unset cookie in PHP?-session-cookies

Accepted answer
Score: 35

Set the cookie's expiration date to a time 4 in the past (like one second after epoch, for 3 example).

setcookie("yourCookie", "yourValue", 1);

This will cause the cookie to expire.

1 is 2 used instead of 0, because 0 sets the cookie 1 to expire at the end of the session.

Score: 14

The solution to this problem was that the 11 I needed to set the correct path to unset 10 the cookie since I was unsetting it from 9 a different file that I originally set it 8 in.

I found out which path I needed to use 7 for the unset by looking for the cookie 6 inside my browser cookies, and once I found 5 the cookie inside my browser, the path was 4 listed near the cookie. So I then set the 3 path to the cookie like so:

setcookie("user_id", $user_id, time() - 1, "/social_learning/site_pages");

The last parameter 2 is the path. And it worked.

My original setcookie looks 1 like this:

setcookie("user_id", $user_id, time() + 7200, "");
Score: 6

There are few security concerns regarding 5 you code, however to answer your question, to 4 unset a cookie in php, all you need to do 3 is to set expiration time to a time in the 2 past:

setcookie("user_id", "", time()-10, "/");

"loginform.php" is not a valid domain, that 1 might be the problem here.

Score: 5

Look at the php manual for information on 14 setcookie


These notes should explain the 13 process:

bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

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

Because 4 setting a cookie with a value of FALSE will 3 try to delete the cookie, you should not 2 use boolean values. Instead, use 0 for FALSE and 1 1 for TRUE.

Score: 4

use this code

  setcookie("CookieName", "", time()-(60*60*24), "/");

works everytime for me in every 1 website

Score: 1

In php manual, you can delete a cookie by 4 setting a expiration date is in the past:


In 3 some case, you should provide path and domain 2 for arguments.

In fact, if you assign a cookie 1 with a empty string, it'll also be unset:


More Related questions