How to set a persistent cookie

I am using php code and trying to set cookies as below:

setcookie("_GuestID",$userID,time() + (20 * 365 * 24 * 60 * 60)); 

I found that cookies expire immediately after closing the browser. I want to make it stubborn for a long time. How can i do this. Please give your suggestions.

thanks

+6
source share
2 answers

As already noted, check if the cookie is really set in your browser (your syntax is displayed correctly).

Cookies will only be saved until you set them. I always used the year as a round period unless there are specific expiration requirements (which are usually much shorter).

Use the strtotime function to make them easier to read:

 setcookie( "cookieName1", $value1, strtotime( '+1 year' ) ); setcookie( "cookieName2", $value2, strtotime( '+30 days' ) ); 

There are many examples of how to use them on the setcookie manual page, which is worth the time to read.

+5
source

There is no special way to set persistent cookies. In the same way, you set regular cookies. Cookies with an expiration date are called persistent.

+2
source

All Articles