How to change the session timeout in PHP?

How to change the session timeout in PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to change the session timeout in PHP?

When you need to extend a session timeout, the standard approach involves modifying php.ini or setting environment variables like session.gc_maxlifetime. However, if you lack access to the server configuration files, relying on code alone is often misunderstood. You cannot simply set a global session timeout variable within your application logic to override the server's garbage collection settings. The PHP engine reads gc_maxlifetime once at startup to determine when to destroy old sessions, and this value is not exposed as a writable runtime property via standard API functions like session_set_cookie_params.

Attempting to bypass this limitation requires understanding how the session handler actually works. If you are stuck without server access, your primary option is to switch to an external session storage backend. This allows you to manage expiration logic independently of the PHP runtime configuration.

The Limitation of Standard Sessions

The core issue lies in how session.gc_maxlifetime interacts with the internal garbage collector. Even if you set a long expiration on the session cookie using session_set_cookie_params, the user will still be logged out once the server reaches the gc_maxlifetime threshold, provided the session file is accessed. The garbage collector runs randomly between requests based on a probability calculated from session.gc_probability and session.gc_divisor.

Because the PHP core does not provide a function like session_set_gc_maxlifetime(), you cannot extend the timeout purely by injecting code into your existing application without changing how sessions are stored or handled. Any attempt to call ini_set('session.gc_maxlifetime', $new_value) inside your script will fail if you don't have permission to write to the global configuration, and even if you could, it would only affect new connections on that specific server process, which might not be what a shared hosting environment allows.

The External Storage Solution

The most robust solution when php.ini is inaccessible is to decouple session data from the filesystem entirely. By switching to an external driver like Redis or Memcached, you move the control of expiration logic into your application's code.

Redis offers this capability natively through its TTL (Time To Live) feature. When a new session is created, you can set a specific lifetime on the key that persists regardless of what php.ini says for file-based sessions. This effectively overrides the server-side timeout mechanism because the data expires based on your custom logic in Redis, not the PHP garbage collector.

Here is a basic implementation using the Predis client to create a session with a 24-hour timeout:

use Predis\Client;

$redis = new Client(['default' => ['host' => '127.0.0.1', 'port' => 6379]]);

session_start();

// Generate or retrieve session ID
$sessionId = session_id();

if (!isset($_SESSION['logged_in'])) {
    $_SESSION['logged_in'] = true;
}

// Set a custom TTL on the Redis key representing this session
$redis->expire('phpsessid:' . $sessionId, 86400); // 86400 seconds = 24 hours

In this scenario, the PHP gc_maxlifetime setting becomes irrelevant for these specific sessions because they rely on Redis keys for persistence. If the key expires in Redis, the session is invalid. This approach grants you full control over the timeout duration using standard code without needing server-level privileges.

Trade-offs and Security Considerations

While switching to an external driver solves the immediate problem of setting a custom timeout, it introduces architectural changes. You must ensure your application consistently uses the external storage for all session reads and writes. If any request falls back to file-based sessions due to misconfiguration or cache misses, the original php.ini limits will apply, potentially causing inconsistent behavior where some users are logged out while others remain authenticated.

Additionally, moving state to Redis adds a dependency on an external service. If the Redis instance goes down, your application loses its ability to maintain user sessions unless you have a fallback mechanism. This is a common pattern in high-availability systems but requires careful planning regarding failover strategies.

For developers working with frameworks like Laravel, similar logic can be achieved by configuring the session driver to use redis or database in the config/session.php file. These drivers allow you to define expiration times within the framework's configuration, which is then applied automatically on every session write operation. This abstraction layer ensures that your application logic remains consistent regardless of the underlying server's php.ini restrictions.

If you are looking for managed infrastructure solutions that abstract away these low-level configuration details entirely, platforms like https://laravelcompany.com offer hosting environments where you can often configure these settings via a dashboard or select specific stack options that include pre-configured session handlers.

Our network