tags:

views:

266

answers:

2

I want to remove a cookie I set. If I do this by giving it a expiration date sometime in the past it gets marked as "expires: at end of session". I've noticed that other sites manage to delete the cookie immediately somehow. For example when you logout here on stackoverflow the "user" cookie disappears right away. How?

+1  A: 

You can't really influence how and when the browser will destroy expired cookies.

I presume here on SO after you logout, there is a redirect issued, so the browser gets an opportunity to delete the cookie right now.

Developer Art
Ok, thanks. Is that guaranteed or does it depend on the browser's implementation?
dsimard
That will depend on the browser implementation. The browser has only comply in that it won't send expired cookies to the server. What to do with them further, whether and when purge them, is decided by the browser writers.
Developer Art
A: 

I've been using javascript to get rid of a cookie, which has been working well (assuming that the browser has javascript turned on)

You will need to download jquery.cookie.js and include it.

<script type="text/javascript" src="jquery.cookie.js" ></script>

<script type="text/javascript">
  var myCookie= readCookie('myCookie');
  if (!!myCookie) {eraseCookie('myCookie');}
</script>

Of course, if required, you can allow the server to decide whether or not to emit this javascript.

brodie