Jump to content

Delete Cookie (minor "bug")


MathieuF

Recommended Posts

So been doing the tutorial for a while now

and i noticed something small in the delete cookie tutorial, the cookie never gets "deleted" I've checked it with the debugger and if you solely use

setcookie("user", "", time() - 3600);

Nothing changes and the cookie still exists $_cookie still hold the the value (i've refreshed several times and even closed my browser)

 

But adding the line

 unset($_COOKIE['user']);

Does remove the cookie properly

Link to comment
Share on other sites

Using setcookie() will not delete the cookie data from the current PHP execution, but it will sent data to the client telling the browser to mark the cookie as expired.

 

Also remember that calling setcookie() to delete a cookie will only work if all the parameters (except value and expires) are exactly the same as the ones used when the cookie was set.

Link to comment
Share on other sites

Also, because of how cookies work, if you call setcookie on a particular page to create a new cookie, that data will not be inside $_COOKIE. That's the same issue that you're seeing with deleting a cookie. Cookie data affects what happens on later pages, not the current page.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...