Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Delete Session Cookie?

How to dynamically, via jQuey, delete a session cookie, without manually restarting the browser?

I read somewhere that session cookie is retained in browser memory and will be removed when the browser is closed.

// sessionFooCookie is session cookie
// this code does not delete the cookie while the browser is still on
jQuery.cookie('sessionFooCookie', null);

More Info: The code snippet above is a javascript code snippet, using jQuery and its jQuery.cookie plugin.

like image 489
Nordin Avatar asked Jul 06 '09 07:07

Nordin


People also ask

How do you remove a session cookie?

If you want to unset all of the values, you can just run a session_destroy() . It will unset all the values and destroy the session.

When should I delete session cookies?

A session cookie is just a normal cookie without an expiration date. Those are handled by the browser to be valid until the window is closed or program is quit.


5 Answers

A session cookie is just a normal cookie without an expiration date. Those are handled by the browser to be valid until the window is closed or program is quit.

But if the cookie is a httpOnly cookie (a cookie with the httpOnly parameter set), you cannot read, change or delete it from outside of HTTP (meaning it must be changed on the server).

like image 187
Gumbo Avatar answered Oct 06 '22 03:10

Gumbo


Be sure to supply the exact same path as when you set it, i.e.

Setting:

$.cookie('foo','bar', {path: '/'});

Removing:

$.cookie('foo', null, {path: '/'});

Note that

$.cookie('foo', null); 

will NOT work, since it is actually not the same cookie.

Hope that helps. The same goes for the other options in the hash

like image 25
Mads Buus Avatar answered Oct 06 '22 01:10

Mads Buus


There are known issues with IE and Opera not removing session cookies when setting the expire date to the past (which is what the jQuery cookie plugin does)

This works fine in Safari and Mozilla/FireFox.

like image 21
Philippe Leybaert Avatar answered Oct 06 '22 01:10

Philippe Leybaert


This needs to be done on the server-side, where the cookie was issued.

like image 29
Josh Stodola Avatar answered Oct 06 '22 01:10

Josh Stodola


you can do this by setting the date of expiry to yesterday.

My new set of posts about cookies in JavaScript could help you.

http://www.markusnordhaus.de/2012/01/20/using-cookies-in-javascript-part-1/

like image 40
Markus Nordhaus Avatar answered Oct 06 '22 02:10

Markus Nordhaus