Shakti Singh
Shakti Singh

Reputation: 86336

When does a cookie with expiration time 'At end of session' expire?

There is a session cookie with expiration time which says 'At end of session'. When exactly does it expire or will it be alive forever?

Upvotes: 154

Views: 237038

Answers (5)

Aris
Aris

Reputation: 5057

If I set a session cookie, then close the tab, then also close the browser and restart, the cookie is still present.

This is really a browser configuration, see here: Chrome doesn't delete session cookies

I tested this with Chrome and Firefox in Fedora Linux.

Upvotes: 3

mingos
mingos

Reputation: 24502

When you use setcookie, you can either set the expiration time to 0 or simply omit the parameter - the cookie will then expire at the end of session (ie, when you close the browser).

Upvotes: 79

James
James

Reputation: 6103

Cookies that 'expire at end of the session' expire unpredictably from the user's perspective!

On iOS with Safari they expire whenever you switch apps!

On Android with Chrome they don't expire when you close the browser.

On Windows desktop running Chrome they expire when you close the browser. That's not when you close your website's tab; its when you close all tabs. Nor do they expire if there are any other browser windows open. If users run web apps as windows they might not even know they are browser windows. So your cookie's life depends on what the user is doing with some apparently unrelated app.

Upvotes: 128

J.Smith
J.Smith

Reputation: 321

Just to correct mingos' answer:

If you set the expiration time to 0, the cookie won't be created at all. I've tested this on Google Chrome at least, and when set to 0 that was the result. The cookie, I guess, expires immediately after creation.

To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.

Example:

Instead of:

document.cookie = "cookie_name=cookie_value; 0; path=/";

Just write:

document.cookie = "cookie_name=cookie_value; path=/";

Upvotes: 20

shamittomar
shamittomar

Reputation: 46692

End of the user session means when the browser is shut down.

Read this: http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age

Upvotes: 10

Related Questions