GoAdmio

sessioncookielifetime

Show or set PHP's session.cookie_lifetime (seconds the visitor's browser keeps the session cookie; 0 means until the browser closes) 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.cookie_lifetime is the number of seconds PHP tells the visitor's browser to keep the session cookie. PHP's own default is 0, which means the cookie is deleted when the browser closes. With a larger value, visitors stay logged in after closing and reopening the browser.

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

[!WARNING] Running the command with no value does not keep PHP's default. It sets 28800, so every visitor's session cookie then lasts 8 hours, even after the browser is closed. To go back to PHP's default, run /go/sessioncookielifetime 0.

This setting controls the cookie in the browser. How long the session data is kept on the server is a separate setting, sessiongcmaxlifetime. To keep visitors logged in for a given time, set both to at least that time.


Quick start

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

# Set one day
/go/sessioncookielifetime 86400

# Back to PHP's default: cookie ends when the browser closes
/go/sessioncookielifetime 0

# Show the current value
/go/sessioncookielifetime return

Syntax

/go/sessioncookielifetime [seconds]
/go/sessioncookielifetime return

The command writes the call and a timestamp to /var/log/go/sessioncookielifetime.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. 0 means until the browser closes.
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.
0 Sets 0: the cookie ends when the browser closes.
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
0 Until the browser closes (PHP's own default)
3600 1 hour
28800 8 hours (this command's default)
86400 1 day
604800 7 days
2592000 30 days

Output

Setting a value

session.cookie_lifetime 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 visitors' cookies for 8 hours on every PHP version
/go/sessioncookielifetime
# Keep visitors logged in for one day (set the server-side lifetime too)
/go/sessioncookielifetime 86400
/go/sessiongcmaxlifetime 86400
# End the cookie when the browser closes
/go/sessioncookielifetime 0
# Read the current value into a script variable
Value=$(/go/sessioncookielifetime return)
echo "$Value"
# Review recent calls
tail /var/log/go/sessioncookielifetime.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.cookie_lifetime line starting in column 1, so PHP uses its built-in default (0). 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 3600 7200 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.
Visitors are still logged out after the cookie time you set The server-side session data is deleted sooner. Set sessiongcmaxlifetime to the same value or higher.
Visitors are still logged out when the browser closes The change hasn't been reloaded yet, or the site sets its own cookie lifetime. Wait a minute, then check the site's code and settings. See Overrides.
Sites on Apache mod_php don't see the change Only a PHP-FPM reload is scheduled. Run reload.

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.cookie_lifetime = line, replaces it. Otherwise adds a new line at the end.
  4. Prints session.cookie_lifetime 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["sessioncookielifetime [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() or session_set_cookie_params() in its code, a .user.ini file, or a PHP-FPM pool setting. Many applications and frameworks set their own session cookie lifetime, and those settings take priority over what this command writes.

Security

A longer cookie lifetime keeps visitors logged in on shared or public computers after the browser is closed. Use a long value only on sites where that is acceptable, and keep it short for admin areas.


See also

Command Description
sessiongcmaxlifetime Read or set PHP session.gc_maxlifetime 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