Change Cookie expiry date - HTTP

10,448

Solution 1

You can only overwrite a cookie if you use the same parameters (i.e. Domain, Path, Secure, and HTTPOnly).

So to overwrite your current cookie, you need to set it from the same domain and path.

Solution 2

If the match is present and unique, this works to set expiration to end of session:

document.cookie="meteor_login_token="+/meteor_login_token=([^;]*)/.exec(document.cookie)[1]+";path=/"
Share:
10,448
RadiantHex
Author by

RadiantHex

hello! :)

Updated on June 04, 2022

Comments

  • RadiantHex
    RadiantHex almost 2 years

    I need to change the expiry date of a Cookie that I occasionally set.

    More precisely I would like to refresh the expiry date to +1 hour.


    This is how I set the cookie

    Set-Cookie: test=123; Expires=Wed, 02 Feb 2011 12:00:00 GMT
    

    Setting the cookie again adds another cookie with the same name :S


    Any ideas?

  • RadiantHex
    RadiantHex about 13 years
    Does the path really need to be exactly the same?
  • Gumbo
    Gumbo about 13 years
    @RadiantHex: Yes, at least the resulting path needs to be the same: “Instances of the same path and name will overwrite each other, with the latest instance taking precedence.”
  • Jose_X
    Jose_X about 3 years
    For anyone who doesn't follow the code, here is a simpler example to change the expiration from 20000 seconds after the cookie was first set to 1000 seconds from the new time it is set, ie, to change the expiration time of an existing cookie: [original:] document.cookie= "blah=foobar; max-age=20000; path=/somepath" ...[and to reset the date later:] document.cookie= "blah=foobar; max-age=1000; path=/somepath" ... In the code above, no max-age was added so that makes the cookie a session cookie, but it retains the old value (and is invoked via old name and old path).