Question:
How to set expiration for a cookie in php?
?
2012-06-20 01:41:31 UTC
Hi, i am trying to set a cookie using php. I am using this code:

setcookie("wishlist", $wishlist, mktime(0, 0, 0, 12, 5, 2015));

Now the problem is that when i check the cookies through the developer tools in the browser, it shows the cookie expiration date as Sun, 07 Dec 1969, 03:28:36 GMT. Can you help me out to sort this problem.
Three answers:
?
2012-06-20 06:03:53 UTC
You can use mktime() as u used in your code



You pass mktime() (in this order) the hour, minute, second, month, day, and year that you want to represent, and mktime() returns to you the number of seconds since January 1, 1970.




$y2k = mktime(0,0,0,1,1,2000);

setcookie('name', 'bret', $y2k);

?>



your code is correct . if you are using chrome browser then avoid it and try to run in some other browser coz their ws a bug in chrome because of it shows 07 Dec 1969, 03:28:36 GMT for every website



btw you can also 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.
anonymous
2012-06-20 01:55:29 UTC
I'm always using time() function.

If you want cookies for 1 week:



setcookie("wishlist", $wishlist, time()+(60*60*24*7));
?
2016-10-22 13:23:24 UTC
Methinks you're utilising the setcookie command, yet no longer placing its linked parameters properly. examine the parameter list, that you'll discover on the link given below, as a thanks to ensure what it truly is that you're *no longer* doing! HTH.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...