Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep session alive for 1week without logout PHP, overwriting default server values?

Tags:

php

How to keep session alive for 1week without logout PHP, overwriting default server values?

I need to do this in the PHP code.

like image 343
Elitmiar Avatar asked Jan 15 '10 10:01

Elitmiar


2 Answers

You can set the session lifetime in your script by using the session_set_cookie_params -function call before the session is started. The first argument of the function call defines the session lifetime in seconds relative to the server time (so make sure the server clock is correct):

For example, to make a session last a week:

session_set_cookie_params(3600 * 24 * 7);
session_start();

This will override the setting in the php.ini -file.

Make sure to check up on the function documentation on the PHP -site: http://www.php.net/manual/en/function.session-set-cookie-params.php

like image 92
TuomasR Avatar answered Sep 28 '22 17:09

TuomasR


Just wanted to add this even though it's an old thread for the sake of completeness for future googlers:

Beware if you are on a Debian system.

The /etc/cron.d/php5 script will still clean your /var/lib/php5/ directory containing your sessions based on the php.ini value session.gc_maxlifetime.

like image 38
Jens Lorentsson Avatar answered Sep 28 '22 17:09

Jens Lorentsson