Reputation: 1415
Would this work?
setcookie("TestCookie", $value, time()+1;
So is this correct... from my understanding, you have to add the current time (since it counts from the epoch) and then add how many seconds? like one?
Upvotes: 1
Views: 8745
Reputation: 460
You method of setcookie(my_cookie,"my_cookie_value", time()+1, '/'); is correct. But if you want to play around with it, we know that in 1 hr we have 3600 seconds why not! you can subtract 3599 seconds from 3600 seconds and get you 1 second, and 3598 secs from 3600 secs = 2secs.
//this will expire the cookie in exact 1 second
setcookie(my_cookie,"my_cookie_value",time()+(3600-3599), '/');
//this will expire the cookie in exact 2 second
setcookie(my_cookie,"my_cookie_value",time()+(3600-3598), '/');
Upvotes: 0
Reputation: 1489
Yes, you are doing it right! time()+1
is completely valid.
Here is more information from the PHP manual about cookie expiration time:
This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
But one (1) second is, as commented, a little bit too short to be useful.
Upvotes: 5