I know that I can set expiration date for a cookie, but can I make a cookie expire after some time OR when browser is closed?
Asked
Active
Viewed 2.5k times
7
-
Maybe see also answers of [https://stackoverflow.com/questions/8894243/why-cookies-dont-expire-after-closing-browser](https://stackoverflow.com/questions/8894243/why-cookies-dont-expire-after-closing-browser) and [Wikipedia: HTTP cookie - Expires and Max-Age](https://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age). There are two cases of Cookies depending on whether Expires is set or not. If set, make a cookie expire after some time, or expire when browser is closed if not. – samm Apr 28 '21 at 06:58
1 Answers
12
If you set an expiration date, this will set them to expire on that date, whether the browser is open or closed.
Only non persisted cookies will expire once the browser is closed.
See cookie expiration rules on wikipedia.

Oded
- 489,969
- 99
- 883
- 1,009
-
9I knew about that. I just came with idea to send multiple cookies one with expiration date and other which expire with user session. Checking both will give me desired result – Sergej Andrejev Mar 29 '10 at 10:56
-
1I actually implemented it the same way as @SergejAndrejev where I used two cookies. – Archimedes Trajano Jul 08 '14 at 19:00
-
-
Just out of curiosity, what's the use case? Different things that should expire at different times? – Phillip Harrington Dec 19 '17 at 20:16
-
It was a bank front-end. The user should be logged out when he either closes the browser or after some amount of minutes. If I remember correctly, that was quite some time ago – Sergej Andrejev Dec 26 '17 at 02:36