Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set session lifetime as infinite

How can I set session life time as infinite? There is a settings session.cookie_lifetime, but I couldn't find which value I need to set it for infinite lifetime. I have tried 0 but it will expire once browser closed.

ini_set('session.cookie_lifetime', 0);
like image 546
ARUN Avatar asked Dec 17 '25 01:12

ARUN


2 Answers

Maybe you can try by setting the expiration time to a very big value? :)

And according what I red on the subject, you'll need more than the line you wrote in your question, something like this:

//set cookie lifetime for 100 days (60sec * 60mins * 24hours * 100days)
ini_set('session.cookie_lifetime', 60 * 60 * 24 * 100);
ini_set('session.gc_maxlifetime', 60 * 60 * 24 * 100);
//maybe you want to precise the save path as well
ini_set('session.save_path', '/home/yoursite/sessions');
//then start the session
session_start();

Good luck!

like image 79
Lord Grosse Jeanine Avatar answered Dec 19 '25 13:12

Lord Grosse Jeanine


session.gc_maxlifetime is the value that controls how long the data stored in the session on the server remains valid ($_SESSION dictionary is how you access the data.) The value session.cookie_lifetime controls how long the data in the browser cookie is valid (value of 0 means "until the browser is closed").

Since the original question was about how to make the session to be valid infinitely, the precise answer would be to change the session.gc_probability value to 0, which means that the garbage collector will never be invoked. (Of course, make sure that that's what you really want to do.)

These values can also be set in php.ini.

session.gc_probability reference

Hope this helps.

like image 43
vp124 Avatar answered Dec 19 '25 15:12

vp124



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!