icant
icant

Reputation: 888

Why is the cookie not removed with php?

I'm trying to remove a cookie with php but it won't work

setcookie("user", 'abc', time()-3600, '/', 'subdomain.tomline.nl', false, true);
setcookie("hash", 'abc', time()-3600, '/', 'subdomain.tomline.nl', false, true);

It's set with the following code, and the file where it is being set is in the same directory as the file where it has to be removed.

setcookie("user", $_POST['mail'], time()+7*24*60*60, '/', 'subdomain.tomline.nl', false, true);
setcookie("hash", $encryptedpw, time()+7*24*60*60, '/', 'subdomain.tomline.nl', false, true);

For privacy reasons I have changed the subdomain. The site is not meant to be publicly visible yet. The cookie is being set successfully but when I want to remove it it still exists and still has the value of the set cookie, not even 'abc' what it's said to have in the remove script.

Upvotes: 0

Views: 443

Answers (3)

pyasi
pyasi

Reputation: 176

Instead of setting it to 'abc' You can set it to just be blank '' and that should remove the cookie as long as you are coming from a matching URL

Upvotes: 0

Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

  session_destroy();

is one idea.

Try setting the path if the URLs are different.

Upvotes: 0

Marc B
Marc B

Reputation: 360762

Don't use time() - 3600 to delete cookies. You have no control over the user's clock, and that's what their browser will use to compare the cookie's expiration date to local time. Particularly if they're more than one timezone away from you.

Set an absolute "can't possibly be using this as a time" value, such as Jan 2 1970 00:00:00 for the date/time.

Upvotes: 4

Related Questions