Set a Cookie to Never Expire

Set a cookie to never expire

All cookies expire as per the cookie specification, so this is not a PHP limitation.

Use a far future date. For example, set a cookie that expires in ten years:

setcookie(
"CookieName",
"CookieValue",
time() + (10 * 365 * 24 * 60 * 60)
);

Note that if you set a date past 2038 in 32-bit PHP, the number will wrap around and you'll get a cookie that expires instantly.

Javascript Cookie with no expiration date

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

How to set never-expiring cookie in ASP.NET Core?

I decided to hardcode the value like this:

var co = new CookieOptions();
co.Expires = new DateTimeOffset(2038, 1, 1, 0, 0, 0, TimeSpan.FromHours(0));

That avoids the 2038 problem.

Cookie Expiry Date Not Set Correctly in Javascript

This is due to Safari's Intelligent Tracking Prevention. It limits certain cookies to 7 day expiration. From What Restricts Does ITP Place on Cookies and Other Web Browser Storage?:

  • First-party cookies created by JavaScript’s document.cookie will expire in 7 days. If the cookies are accessed within those 7 days, then their expiration date will be extended by 7 days.
  • First-party cookies created by JavaScript’s document.cookie AND are created by a tracking domain AND use link decoration will expire in 24 hours. If the cookies are accessed within 24 hours, then their expiration date will be extended by another 24 hours.

So even though the expiration is set to 1 week, if the user continues to access the site that uses the cookie, it will be extended automatically.

Set cookie/session that never expires even on browser close using Google App Engine (Python)

I was experiencing a caching issue which prevented my session cookie from showing in the developer console and being applied. After erasing the browser history and restarting my computer, it cleared the cache. Eventually I got it to work with the following:

from webapp2

class MainHandler(webapp2.RequestHandler):
def get(self):
the_value = ''.join(SystemRandom().choice(string.digits _ string.ascii_lowercase) for_in range (50))
max_age = 2147483647
# max age for Internet Explorer
cookie_expires = datetime.datetime.now() + datetime.timedelta (200)
return self.response.set_cookie('my_cookie', the_value, max_age=max_age, httponly=True, secure=True)

NOTE: if you want to apply this change to a specific cookie, rename "my_cookie" to that cookie (i.e. "SACSID" a cookie on many Google applications)

Postman receive cookies with expires: never

A few things are wrong here.

  • You're not setting the expiry correctly: the property is named expires.
  • Using the + operator on Date doesn't add time, it converts to a string and concatenates a number onto the end, which is not what you want.
  • Check your math. JavaScript date units are typically milliseconds. Do you really want this cookie to expire 1200 milliseconds (1.2 seconds) after it's set?

This will fix the first two points:

res.cookie('abc@d.com', 'foo', { expires: new Date(Date.now() + 1200), httpOnly: true })


Related Topics



Leave a reply



Submit