GoAdmio

sessiongcmaxlifetime

Show or set PHP's session.gc_maxlifetime (seconds before an idle PHP session can be deleted) in every php.ini on the server, then reload PHP-FPM

On this page: Overview · Quick start · Syntax · Arguments · Output · Examples · Troubleshooting · Details · See also


Overview

session.gc_maxlifetime is the number of seconds PHP waits after a session was last used before its data counts as garbage and can be deleted. Visitors whose session is deleted are logged out. PHP's own default is 1440 (24 minutes), which is short for sites with logins.

/go/sessiongcmaxlifetime changes this one setting in every php.ini file on the server (all installed PHP versions) with a single command. It can:

This setting controls how long session data is kept on the server. It does not control how long the visitor's browser keeps the session cookie. That is sessioncookielifetime.


Quick start

# Set 8 hours (the default value)
/go/sessiongcmaxlifetime

# Set one day
/go/sessiongcmaxlifetime 86400

# Show the current value
/go/sessiongcmaxlifetime return

Syntax

/go/sessiongcmaxlifetime [seconds]
/go/sessiongcmaxlifetime return

The command writes the call and a timestamp to /var/log/go/sessiongcmaxlifetime.log. Run it as root, because one of its steps does not use sudo (see Troubleshooting).


Arguments

Argument Required Default Notes
seconds No 28800 The new value, in seconds.
return No — Print the current value and change nothing. Case-insensitive.
You pass Result
Nothing Sets 28800 (8 hours).
An option starting with -- Ignored. Sets 28800.
A number, such as 86400 Sets that number.
return, Return, or RETURN Prints the current value.
More than one word All the words are joined with spaces and written as the value.

[!WARNING] The value is not checked. Text, a negative number, or several arguments are all written to php.ini as they are, which can leave PHP with an invalid setting. Pass a single whole number.

Common values

Seconds Length
1440 24 minutes (PHP's own default)
3600 1 hour
28800 8 hours (this command's default)
86400 1 day
604800 7 days

Output

Setting a value

session.gc_maxlifetime updated successfully.

This line is printed even if no php.ini file was found or changed. It does not mean the setting was verified.

Reading the value

28800

Only the number is printed. See How return works.

Unknown operating system

Unknown operating system.

Nothing is changed.


Examples

# Keep sessions for 8 hours on every PHP version
/go/sessiongcmaxlifetime
# Keep sessions for one day
/go/sessiongcmaxlifetime 86400
# Read the current value into a script variable
Value=$(/go/sessiongcmaxlifetime return)
echo "$Value"
# Review recent calls
tail /var/log/go/sessiongcmaxlifetime.log

Troubleshooting

Symptom Likely cause Fix
Unknown operating system. The OS name in /usr/local/goadmio/linux/os.db is not AlmaLinux, Rocky, CentOS, Red Hat, Fedora, Debian, or Ubuntu. Edit the php.ini files by hand.
return prints an empty line The first php.ini has no active session.gc_maxlifetime line starting in column 1, so PHP uses its built-in default (1440). Set a value with the command.
return ends with exit status 1 This is how the script exits after printing. It is not a failure. In scripts, read the output, not the exit status.
The value written looks wrong, such as 1440 3600 More than one argument was passed. Pass one number.
Permission denied when a line is added A missing setting is appended without sudo. Run the command as root.
"updated successfully" shown, but nothing changed No php.ini files were found at the expected paths. Check that PHP is installed under /etc/php or /etc/opt/remi.
Sites on Apache mod_php don't see the change Only a PHP-FPM reload is scheduled. Run reload.
Sessions still end at the old time The reload hasn't run yet, a site overrides the setting (ini_set(), .user.ini, or a PHP-FPM pool), or the site's own code deletes sessions. Wait a minute, then check the site's settings.
Visitors are logged out when they close the browser That is the cookie lifetime, not this setting. Use sessioncookielifetime.

Details

Which files are changed

The command builds a list of php.ini files from the operating system and skips any that don't exist.

Operating system Files
AlmaLinux, Rocky, CentOS, Red Hat, Fedora /etc/php.ini, and /etc/opt/remi/<version>/php.ini for each folder in /etc/opt/remi
Debian, Ubuntu /etc/php.ini, and /etc/php/<version>/fpm/php.ini, /etc/php/<version>/cli/php.ini, and /etc/php/<version>/apache2/php.ini for each folder in /etc/php
Anything else Prints Unknown operating system. and stops

What it does

  1. Records the call in the log.
  2. Reads the OS name and builds the list of php.ini files.
  3. For each file that exists:
    1. Reads the current value from the first matching line.
    2. If the first argument is return, prints it and stops (see below).
    3. Chooses the new value: the default 28800 if there is no argument or it starts with --, otherwise the arguments as typed.
    4. If the file has an active session.gc_maxlifetime = line, replaces it. Otherwise adds a new line at the end.
  4. Prints session.gc_maxlifetime updated successfully.
  5. Schedules /go/fpm reload to run in one minute, unless a fpm reload job is already waiting in the at queue.
flowchart TD
    A["sessiongcmaxlifetime [value]"] --> B["Log, detect OS, list php.ini files"]
    B --> C{"First argument is return?"}
    C -- "Yes" --> D["Print value from first file found, exit status 1"]
    C -- "No" --> E["Choose value: 28800 or the arguments"]
    E --> F["For each existing php.ini: replace the line, or append it"]
    F --> G["Print updated successfully"]
    G --> H["Schedule fpm reload in 1 minute, if none is pending"]

How return works

return reads the value from the first php.ini file that exists, which is /etc/php.ini when that file is present, and prints only that value. Other php.ini files may hold a different value. The command then ends with exit status 1.

The value is read only from a line that begins at the very start of the line. A line that is indented or commented out with ; is not seen.

When the change takes effect

Where When
PHP-FPM sites About one minute later, after the scheduled /go/fpm reload.
Command-line PHP (php in a terminal or cron) The next time PHP starts.
Apache mod_php Not reloaded by this command. Run reload.

The reload is scheduled with at, so the atd service must be running. If a reload is already queued, no second one is added.

Overrides

A php.ini value can be overridden for a single site by ini_set() in its code, a .user.ini file, or a PHP-FPM pool setting. Those take priority over what this command writes.


See also

Command Description
sessioncookielifetime Read or set PHP session.cookie_lifetime across all php.ini files
session Create and set permissions on PHP session directories
clearsession Delete all PHP session files on the server, logging out every site visitor
fpm Restart all installed PHP-FPM versions on your server
reload Reload PHP-FPM and the active web server
maxexecutiontime Change the max execution time for all PHP versions
memorylimit Change the PHP memory limit for all PHP versions
multiphp Change the PHP version of one domain or all domains