2

How do I set up a cookie that won't never expire, even when the user closes his browser or stops his machine?

Thanks!

asked Jul 28, 2011 at 20:28
1
  • 4
    This is pretty clearly explained right on the setcookie docs page. Commented Jul 28, 2011 at 20:31

5 Answers 5

5

I don't think there is a way to have a 'permanent' cookie, but rather one that expires very very far into the future. Just because you remained logged into a website/service the next time you return or after you close your browser, does not mean that there is no expiration date on the cookie.

Check out the PHP documentation on setting the lifetime of a cookie.

answered Jul 28, 2011 at 20:30
Sign up to request clarification or add additional context in comments.

4 Comments

In facebook for example you stay logged even when you shut down your machine or you close the browser. The cookie is there until you click on logout. How do I make the very same thing?
See my update - just because you remain logged into Facebook doesn't mean they have a 'permanent' cookie. I can't speak to its exact design and implementation, but it is very likely set to expire at a date very far into the future.
@DiegoP: There are a bunch of Facebook cookies. Some expire when I close the window, one expires next week, a few expire next month, and a couple expire in two years, but no FB cookie will last forever.
If you have a reason to be nasty, you could use evercookie for an extremely durable cookie.
2

setcookie("example", $cookievalue, time()+60*60*24*6004, "/", ".example.com");

Impossible to set a cookie forever, browsers wouldn't allow that. (ps. it's amazing what wonders google will conjure up if you were to literally google the title of this question)

answered Jul 28, 2011 at 20:30

4 Comments

Bound to fail for 32bit systems using 32bit time_t's. It's not presently safe to set a date beyond early 2038 at present. Still far away, but don't count on the user having a 64bit system that does support years >2038.
so what happens? the entire cookie setting fails?
depends on the underlying system and how they process dates. could be an integer overflow, could get truncated and end up with a much "earlier" date than expected, etc...
By the time @MarcB's concern becomes a problem (on 2021年08月12日), by the current looks it seems everyone would've migrated to a 64 bit system.
2

You can assign a expiration date which is very far from now:

setcookie("testCookie","value",mktime (0, 0, 0, 12, 31, 2015));
//Expires on 31 december 2015
answered Jul 28, 2011 at 20:30

2 Comments

2015 is about as far away as 2000 was for all those "let's only use 2 digits for years" people...
At first I thought, "but this is only 21 days in the future?" And then I looked at the date the answer was posted. :)
0
setcookie("cname" ,$value, mktime (0, 0, 0, 12, 31, 2015));
answered Jul 28, 2011 at 20:31

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.
0

The user will always have the option to remove or block cookies. But you can always set the expire date in the far future.

answered Jul 28, 2011 at 20:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.