How to keep a user logged in with PHP Sessions for 1 year?

Tags: ,

I’m currently using the following code:

ini_set('session.gc_maxlifetime', 31968000);
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 1);
ini_set('session.cookie_lifetime', 31968000);

if(isset($_COOKIE[session_name()])) {
    setcookie(session_name(), $_COOKIE[session_name()], time() + 31968000, "/", ".********.net");

I’m using that code for the “landing page” after someone logged in or registered successfully (after registration you’ll be automatically logged in and transferred to the “landing page”)

For some reason this works for a couple of minutes. I can close the App Safari and open it again for a few seconds and then maybe after half an hour it stops working. First I thought it would be working.

Any ideas?

Btw, 31968000 is more than one year of time in seconds.


I had to change the properties session.gc_maxlifetime, session.gc_probability, session.gc_divisor and session.cookie_lifetime in the php.ini file in order to make it work.

Also it’s important to restart the Apache server and php-fpm to load the edited php.ini file.

service apache2 restart

service php7.3-fpm restart

Source: stackoverflow