Reputation: 10258
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.
Upvotes: 183
Views: 249100
Reputation: 1893
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.
Upvotes: 2
Reputation: 488714
Nope. That can't be done. The best 'way' of doing that is just making the expiration date be like 2100.
Edit
As of August 2022, Chrome cookies can be set to expire after 400 days only.
Upvotes: 214
Reputation: 1077
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.
Upvotes: 0
Reputation: 87
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.
Upvotes: 0
Reputation: 1013
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.
Upvotes: 3
Reputation: 8720
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 });
Upvotes: 19
Reputation: 1739
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.
Upvotes: 1
Reputation: 5839
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 :)
Upvotes: 91
Reputation: 131
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.
Upvotes: 13
Reputation: 741
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
Upvotes: 14
Reputation: 2114
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).
Upvotes: 119