When I want to remove a Cookie I try
unset($_COOKIE['hello']);
I see in my cookie browser from firefox that the cookie still exists. How can I really remove the cookie?
When I want to remove a Cookie I try
unset($_COOKIE['hello']);
I see in my cookie browser from firefox that the cookie still exists. How can I really remove the cookie?
To reliably delete a cookie it's not enough to set it to expire anytime in the past, as computed by your PHP server. This is because client computers can and often do have times which differ from that of your server.
The best practice is to overwrite the current cookie with a blank cookie which expires one second
in the futureafter the epoch (1 January 1970 00:00:00 UTC), as so:You have to delete cookies with php in your server and also with js for your browser.. (They has made with php, but cookie files are in the browser client too):
An example:
That will unset the cookie in your code, but since the $_COOKIE variable is refreshed on each request, it'll just come back on the next page request.
To actually get rid of the cookie, set the expiration date in the past:
If you set the cookie to expire in the past, the browser will remove it. See setcookie() delete example at php.net
You May Try this
I know that there has been a long time since this topic has been created but I saw a little mistake within this solution (I can call it like that, because it's a detail). I agree that the better solution is probably this solution:
But, in the present case, you delete the cookies in every case where the unset function works and immediately you create new expired cookies in case that the unset function doesn't work.
That means that even if the unset function works, it will still have 2 cookies on the computer. The asked goal, in a logical point of view, is to delete the cookies if it is possible and if it really isn't, make it expire; to get "the cleanest" result.
So, I think we better should do:
Thanks and have a nice day :)