178

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.

4
  • 1
    Why don't you want make the date large? Feb 10, 2009 at 14:52
  • 15
    making the date large makes me feel as if I was cheating. If that's the only way to go, I guess I will have to make the date large.
    – Jose Vega
    Feb 10, 2009 at 14:56
  • 1
    Newer versions of Chrome are introducing a limit of 400 days: chromestatus.com/feature/4887741241229312 Expiration attributes greater than 400 days in the future will be reduced to the limit.
    – Zeronull
    Jul 25, 2022 at 14:11
  • You can achieve the near equivalent of a forever cookie by sending a cookie with an updated expiration date with every response. This way, as long as a user uses your website once every 400 days, the cookie remains forever (until 2038 at least). And to be honest, is that even a valid use case that users would not visit for over a year?
    – Christoph
    Dec 2, 2022 at 12:59

11 Answers 11

208

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

20
  • 67
    Only four more years to go. Hope you updated the cookie. Mar 30, 2016 at 16:11
  • 215
    2020 - the year of the cookiepocalypse
    – JeffreyPia
    Apr 18, 2016 at 17:57
  • 31
    Damn, it's getting closer every year, I hope your code has been updated.
    – kartsims
    Jan 8, 2018 at 14:44
  • 46
    It's edited to 2030 now, we're postponing our doom 10 more years Feb 23, 2018 at 11:47
  • 20
    So, basically, on 19th Jan 2038, Chewie will sit in the Millennium Falcon and complain about the cookie policy popup on the galactic map API yet again...
    – nickhar
    Jul 16, 2018 at 22:51
118

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).

3
  • 34
    2038-01-19, 03:14:08 UTC, to be precise. Jan 16, 2015 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. Apr 18, 2016 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)
    – mfluehr
    Jul 26, 2019 at 13:29
87

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 :)

3
18

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 });
6
  • 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, 2016 at 18:16
  • 1
    The above code uses the jquery-cookie plugin which has been retired in favor of js-cookie.
    – Jens
    Dec 12, 2018 at 21:41
  • 6
    for vanilla JS: document.cookie = 'subscripted_24=true; expires=' + new Date(2147483647 * 1000).toUTCString(); Oct 3, 2019 at 17:31
  • 1
    @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, 2021 at 23:48
  • 1
    04:14:07 might be BST? I think in UTC it's 2038-01-19T03:14:07+00:00 ?
    – Nick
    Jul 27, 2021 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

1
  • 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, 2019 at 5: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

7
  • 6
    I think you don't realize how far into the future a 64 bit computer can count, namely 292 billion years Mar 23, 2017 at 1:01
  • @GustvandeWal So yeah? I've already read that. I even included that link in my answer.
    – rhavendc
    Mar 24, 2017 at 8: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) Mar 24, 2017 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, 2017 at 7:12
  • 5
    A small note: if we would be using 33-bit processors, then the year 2110 would make sense.
    – hegez
    Apr 8, 2017 at 17:07
2

As said before, not setting an expiration date will give you the exact opposite, as the browser will get rid of it at the end of your session. So you have to set an expiration date to make sure it lasts.

BUT since 09/2022, Chrome limits your cookie max age to 400 days (https://chromestatus.com/feature/4887741241229312):

When cookies are set with an explicit Expires/Max-Age attribute the value will now be capped to no more than 400 days in the future. Previously, there was no limit and cookies could expire as much as multiple millennia in the future.

Motivation

The draft of rfc6265bis now contains an upper limit for Cookie Expires/Max-Age attributes. As written:
"The user agent MUST limit the maximum value of the [Max-Age/Expiration] attribute. The limit MUST NOT be greater than 400 days (34560000 seconds) in duration. The RECOMMENDED limit is 400 days in duration, but the user agent MAY adjust the limit to be less. [Max-Age/Expiration] attributes that are greater than the limit MUST be reduced to the limit."

400 days was chosen as a round number close to 13 months in duration. 13 months was chosen to ensure that sites one visits roughly once a year (e.g., picking health insurance benefits) will continue to work.

So there you go, 400 days is the max lifespan you can shoot for.

1
  • This answer is better since it contains the info regarding Chrome's update. Just a suggestion, but might be more complete and more qualified to replace previous answers if you also included code example.
    – Michael
    Jan 13 at 13:48
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.

4
  • Or the browser is closed or the computer is shut down Oct 27, 2018 at 16:35
  • 1
    @RedwolfPrograms Since when is local storage cleared when browser closes or computer shuts down? Apr 15, 2019 at 9:24
  • @IgorJerosimić Oops! I guess I thought that because of a bug in a site of mine... Apr 15, 2019 at 12:29
  • Is local storage cleared when the browser cache is cleared? If so, that might explain some issues with major sites.
    – Jake
    Feb 20 at 0:47
0

Try Javascript Local Storage

<!DOCTYPE html>
<html>
<body>

<h1>The Window Object</h1>
<h2>The localStorage Property</h2>

<p>Saved name is:</p>
<p id="demo"></p>

<script>
// Set Item
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("demo").innerHTML = 
localStorage.getItem("lastname");
</script>

</body>
</html>

Note: The localStorage object stores data with no expiration date. The data is not deleted when the browser is closed and are available for future sessions.

If you delete cookie in your web browser manually, all local storage data will delete too.

Unlike Cookie, localStorage cannot be access from PHP Code script. This is not recommended if you need login credential that needs verification from server side.

0

You can and should refresh the cookie expiration date whenever a vistor returns to your site.

Just set the cookie again to the same value as before, but with a new expiration date, further in the future. Do this whenever you initialize values from cookies. That way, regular visitors will not lose their settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.