167

I would like to set up a cookie that never expires. Would that even be possible?

 document.cookie = "name=value; expires=date; path=path;domain=domain; secure";

I don't want to make the date really large, I am just wondering if there was a value for the expires parameter on the cookie that told it never to expire.

Thanks.

Jose Vega
  • 9,950
  • 7
  • 39
  • 57

8 Answers8

195

Nope. That can't be done. The best 'way' of doing that is just making the expiration date be like 2100.

Alec
  • 7,110
  • 7
  • 28
  • 53
Paolo Bergantino
  • 466,948
  • 77
  • 516
  • 433
115

There is no syntax for what you want. Not setting expires causes the cookie to expire at the end of the session. The only option is to pick some arbitrarily large value. Be aware that some browsers have problems with dates past 2038 (when unix epoch time exceeds a 32-bit int).

Jamie
  • 2,068
  • 1
  • 15
  • 9
  • 33
    2038-01-19, 03:14:08 UTC, to be precise. – Wilhelm Klopp Jan 16 '15 at 19:14
  • Just to clarify: is that a rule, working the same way in every browser? meaning that if I don't set the expiring date, the cookie will last only to the end of session. – Silver Ringvee Apr 18 '16 at 13:42
  • 3
    A cookie created without an `Expires` or `Max-Age` directive is a session cookie: it is deleted when the client shuts down. However, web browsers may use **session restoring**, which makes most session cookies permanent, as if the browser was never closed. ([source: MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Session_cookies)) – mfluehr Jul 26 '19 at 13:29
83

You can do as the example on Mozilla docs:

 document.cookie = "someCookieName=true; expires=Fri, 31 Dec 9999 23:59:59 GMT";

P.S

Of course, there will be an issue if humanity still uses your code on the first minute of year 10000 :)

Prusprus
  • 7,888
  • 8
  • 41
  • 56
Nimir
  • 5,589
  • 1
  • 24
  • 34
  • 126
    Are you sure its a Friday? – David May 03 '16 at 09:38
  • 9
    https://www.google.com/webhp?sourceid=chrome-instant&rlz=1C1KMZB_enUS560US560&ion=1&espv=2&ie=UTF-8#q=what+day+is+december+31st+9999&* – Adam Nelson Mar 22 '17 at 16:55
  • 7
    Did not work for me using https://www.npmjs.com/package/universal-cookie. Went with the [year 2038 solution](https://stackoverflow.com/a/34586818/2024590). I wrote a ticket to come back in 2030 to see if there is a fix. – Hinrich Mar 28 '19 at 14:12
17

All cookies expire as per the cookie specification, Maximum value you can set is

 2^31 - 1 = 2147483647 = 2038-01-19 04:14:07

So Maximum cookie life time is

$.cookie('subscripted_24', true, { expires: 2147483647 });
Yuseferi
  • 6,789
  • 11
  • 58
  • 92
  • 10
    That number didn't work for me. After a bit of trial and error, the highest number I was able to use was 99983090 (expires=Fri, 12 Sep 275760 18:10:24 GMT). Anything higher returned "Invalid Date" – JeffreyPia Apr 18 '16 at 18:16
  • If you pass a number as expires parameter to jquery-cookie or js-cookie, it is taken as days to expire from now. If you want a fixed date you have to pass a Date object instead. – Dario Seidl Oct 16 '17 at 11:58
  • 1
    The above code uses the [jquery-cookie](https://github.com/carhartl/jquery-cookie) plugin which has been retired in favor of [js-cookie](https://github.com/js-cookie/js-cookie). – Jens Dec 12 '18 at 21:41
  • 6
    for vanilla JS: `document.cookie = 'subscripted_24=true; expires=' + new Date(2147483647 * 1000).toUTCString();` – Oksana Romaniv Oct 03 '19 at 17:31
  • @JeffreyPia You can get a couple extra hours, up until `Sat, 13 Sep 275760 00:00:00 GMT`. Makes all the difference. – Zed Jul 10 '21 at 23:48
  • 04:14:07 might be BST? I think in UTC it's `2038-01-19T03:14:07+00:00` ? – Nick Jul 27 '21 at 11:21
13

You could possibly set a cookie at an expiration date of a month or something and then reassign the cookie every time the user visits the website again

Liam Martens
  • 731
  • 7
  • 21
  • 1
    This is the right answer to set endless cookies. And if you need to take into account that the user never closes the site, you can implement an additional condition for updating cookies by any action or after a certain time. – Alex Shink Nov 30 '19 at 05:01
13

If you don't set an expiration date the cookie will expire at the end of the user's session. I recommend using the date right before unix epoch time will extend passed a 32-bit integer. To put that in the cookie you would use document.cookie = "randomCookie=true; expires=Tue, 19 Jan 2038 03:14:07 UTC;, assuming that randomCookie is the cookie you are setting and true is it's respective value.

3

YOU JUST CAN'T. There's no exact code to use for setting a forever cookie but an old trick will do, like current time + 10 years.

Just a note that any dates beyond January 2038 will doomed you for the cookies (32-bit int) will be deleted instantly. Wish for a miracle that that will be fixed in the near future. For 64-bit int, years around 2110 will be safe. As time goes by, software and hardware will change and may never adapt to older ones (the things we have now) so prepare the now for the future.

See Year 2038 problem

rhavendc
  • 935
  • 9
  • 21
  • 6
    I think you don't realize [how far into the future a 64 bit computer can count](https://en.wikipedia.org/wiki/Year_2038_problem#Solutions), namely 292 billion years – Gust van de Wal Mar 23 '17 at 01:01
  • @GustvandeWal So yeah? I've already read that. I even included that link in my answer. – rhavendc Mar 24 '17 at 08:07
  • 2
    From your answer: For *64-bit int*, years around 2110 will be safe. This seems suspiciously much like you expect 64-bit computers to count twice as far as 32-bit computers (1970 -> 2035 -> 2110) – Gust van de Wal Mar 24 '17 at 14:14
  • @GustvandeWal Sorry if my words sound suspicious. Maybe it's not the best way to say it in english. But hey, I didn't expect or think that kind of idea you've just said. – rhavendc Mar 27 '17 at 07:12
  • Then how did you come up with the idea that 2110 will be safe? – Gust van de Wal Mar 27 '17 at 22:23
  • @GustvandeWal That's just my example for current programs that will make it or will still be in use for more than 25 yrs from now. I just want to say is, somehow your code will not face date problems after the 32-bit date limitation unless in that year, better solutions or techs emerge. – rhavendc Mar 28 '17 at 06:33
  • 5
    A small note: if we would be using 33-bit processors, then the year 2110 would make sense. – hegez Apr 08 '17 at 17:07
1

If you intend to read the data only from the client-side, you can use the local storage. It's deleted only when the browser's cache is cleared.

jfhfhf839
  • 1,451
  • 1
  • 11
  • 11