Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set persistent cookie

Tags:

php

cookies

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

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

I found that cookies are expire just after close of browser. I want to make it persistent for long time.How I can do. Please give your suggestions.

Thanks

like image 963
user1829627 Avatar asked Nov 22 '12 21:11

user1829627


2 Answers

As has already been noted, check if the cookie is actually being set in your browser (your syntax appears correct).

Cookies will only persist for as long as you have set them. I've always used a year as a round period unless there are specific expiry 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 taking the time to read.

like image 110
nickhar Avatar answered Sep 30 '22 22:09

nickhar


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

like image 29
brute_force Avatar answered Sep 30 '22 23:09

brute_force