18

I am trying to increase my php session time to 6 hours.

Here is the code to increase the session time:

ini_set('session.gc_maxlifetime', 60 * 60 * 6); // 6 Hours 

However, it seems to ONLY have a session time of 1 hour.

Any suggestion are greatly appreciated.

Also, how do I test this feature without having to wait 6 hours to see if my session times out.

Ken
  • 2,849
  • 8
  • 24
  • 23
  • is `ini_set` allowed on your system? Are you calling it before `session_start()`? – Czechnology Mar 08 '11 at 20:52
  • 1
    what does the entry for `session.gc_maxlifetime` say when you do a `phpinfo();` on the same page after your `ini_set()` ? – drudge Mar 08 '11 at 20:54
  • I want to create the session timeout to 6 hours but my browser is timing out in 1/2 hour. I am on a PLESK server. I updated .htaccess to have the settings: php_value session.gc_maxlifetime 21600 php_value session.cache_expire 21600 php_value session.cookie_lifetime 21600 Here is the relevant PHPinfo: session.gc_maxlifetime local=21600 master=1440 session.cache_expire local=21600 master=180 session.gc_maxlifetime local=21600 master=1440 – Ken Mar 10 '11 at 19:19
  • - For sharing hosting providers check = https://stackoverflow.com/a/58465785/8010015 – ChriStef Oct 19 '19 at 16:40

6 Answers6

15

The scenario

You’re running Debian Linux or Ubuntu Linux. You want PHP sessions to last longer than the default 1440 seconds (24 minutes). So you do this:

ini_set('session.gc_maxlifetime', 10800);    # 3 hours

With this setting, sessions should remain active for at least three hours, as long as users don’t close their browser.1

But no matter what you do, sessions keep getting deleted after 24–54 minutes. It seems PHP is ignoring the gc_maxlifetime setting.

Why this happens

Debian and Ubuntu Linux override PHP’s session behavior. If you look closely, you’ll see that session.gc_probability is set to 0, meaning PHP’s garbage collection will never run. Instead, there’s a Debian-specific cron job in /etc/cron.d/php5 that runs every 30 minutes!

The cron job does garbage collection based on the global session.gc_maxlifetime in php.ini. The session.gc_maxlifetime in your app is ignored.

The solution

While you could disable the cron job and/or modify php.ini, I’d prefer to fix the problem without modifying system defaults. A better solution is to create your own sessions directory, somewhere outside the normal one, and then locally enable PHP’s session garbage collection.

To do this, set session.gc_maxlifetime, session.gc_probability, session.gc_divisor, and session.save_path:

# Session lifetime of 3 hours
ini_set('session.gc_maxlifetime', 10800);

# Enable session garbage collection with a 1% chance of
# running on each session_start()
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);

# Our own session save path; it must be outside the
# default system save path so Debian's cron job doesn't
# try to clean it up. The web server daemon must have
# read/write permissions to this directory.
session_save_path(APP_PARENT_DIR . '/sessions');

# Start the session
session_start();
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • 2
    This answer appears to be copied from this blog post: http://natesilva.tumblr.com/post/250569350/php-sessions-timeout-too-soon-no-matter-how-you. Are you the author of the blog, or did you just find it and copy the relevant information? – Edward Jan 25 '17 at 20:40
2

Increasing session.gc_maxlifetime via ini_set may not work if there is another script that runs (e.g. an other vhost) that uses the same session.save_path. The other script removes the sessions of all scripts by its own lifetime:

Note:
If different scripts have different values of session.gc_maxlifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data. In this case, use this directive together with session.save_path.

Source: http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime

Also be sure to change the setting before session_start(). If you have session.auto_start enabled, it is to late when you use ini_set.

jpossi
  • 364
  • 2
  • 4
0

Lemp/LAMP (Ubuntu) I solve my issue

Edit: sudo nano /etc/php/7.2/fpm/php.ini

gc_maxlifetime 1440 to any big number

session.gc_maxlifetime = 144000
matinict
  • 2,411
  • 2
  • 26
  • 35
0

In most common cases you need to edit /etc/php.ini and change this (for 6 hours):

session.gc_maxlifetime = 216000

Recently I came across a Rocky Linux 9 installation with PHP 8.x and it reflected the new session.gc_maxlifetime in phpinfo() only after I rebooted the server. Not after I restarted httpd web server service.

For debugging you can also create a PHP page and write <?php phpinfo();?> in it, it will show you the current session.gc_maxlifetime but also all the loaded .ini configuration files, wich might overwrite your value.

adrianTNT
  • 3,671
  • 5
  • 29
  • 35
-2

Try following-

  • Avoid spaces

ini_set('session.gc_maxlifetime', 60*60*6);

Or

  • simply enter values into seconds as...

ini_set('session.gc_maxlifetime', 21600);

Jasmeen
  • 876
  • 9
  • 16
  • 1
    The spaces don't matter, nor do you have to manually multiply the values. Why does this question seem to be attracting such responses? – Leng Nov 13 '13 at 23:24
-23

use this

ini_set('session.gc_maxlifetime', 6 * 60 * 60); // 6 Hours  instead to this 
ini_set('session.gc_maxlifetime', 60 * 60 * 6); // 1 Hours 
ini_set('session.gc_maxlifetime', [hours] * [minutes] * [seconds]); 
shanethehat
  • 15,460
  • 11
  • 57
  • 87