PHP sessions default timeout

PHP sessions default timeout

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Sessions Default Timeout

By default, a standard PHP session will timeout and invalidate after 2 hours of inactivity. This behavior is hardcoded into the core PHP distribution as part of its security model to prevent unauthorized access if a session cookie is stolen or intercepted while left idle. If you rely on the default configuration without modification, your users will be forcibly "logged out" after exactly 120 minutes of no activity, regardless of whether they have interacted with your application during that period.

The mechanism governing this timeout is controlled by the session.gc_maxlifetime directive within your php.ini file. This setting dictates how many seconds a session must exist before the garbage collector considers it abandoned and deletes the associated data from the storage driver (whether that be files in C:\wamp\temp\, /tmp/, or a database).

; Default configuration in php.ini
session.gc_maxlifetime = 1440

The value 1440 represents seconds. When divided by 60, this confirms the standard 2-hour window. It is crucial to distinguish between this garbage collector lifetime and the browser cookie expiration. The session timeout only triggers when both conditions are met: the PHP script must recognize the session as expired based on the last activity timestamp, and the client's browser must still be willing to send the session ID cookie with subsequent requests.

If your session.cookie_lifetime is set to -1 (the default), the cookie is a "session cookie." This means it exists only until the browser closes. In this specific scenario, even if you extend session.gc_maxlifetime to 86400 seconds (24 hours) in your PHP configuration, a user who leaves their browser open but inactive for 2 hours will be logged out when they refresh the page or navigate away and back. The server accepts the cookie, but the session data has already been purged by the garbage collector at the 120-minute mark, resulting in a session_start() failure on the next request.

Conversely, if you set session.cookie_lifetime to match your PHP timeout (e.g., 86400), the user could theoretically remain logged in for 24 hours of total time since login, provided they make at least one request within that window to reset the "last activity" timestamp. However, this approach shifts the responsibility of logout management entirely to the session duration rather than user inactivity.

Implementing Custom Timeouts

To alter this behavior, you must adjust the php.ini directive or override it dynamically in your application entry point. In a framework like Laravel, you can modify the session lifetime within your bootstrap process or via configuration files:

// Example: Setting session timeout to 1 hour (3600 seconds)
$config = [
    'session' => [
        'lifetime' => 3600, // Seconds until data is garbage collected
        'expire_on_close' => false, // Keep session alive even if connection drops
    ]
];

However, simply changing the server-side timeout does not guarantee a seamless user experience. You must also ensure your frontend logic aligns with this change. If you rely on local storage tokens or remember-me functionality, those lifetimes often conflict with PHP session settings. For instance, a "Remember Me" checkbox might store a token valid for 30 days, while the PHP session expires in 2 hours. This creates a mismatch where the user is technically authenticated but cannot access protected resources until they refresh their browser or clear cookies.

Security Implications and Trade-offs

Extending the default timeout beyond 2 hours increases the window of opportunity for session hijacking. If an attacker manages to sniff a session ID via a man-in-the-middle attack or XSS vulnerability, they can use that stolen ID indefinitely if the timeout is set too long. The 2-hour default is a deliberate trade-off between user convenience and security posture.

Reducing the timeout below the default (e.g., setting it to 15 minutes) significantly improves security but degrades the user experience, forcing frequent re-authentication. This is often acceptable for public Wi-Fi networks or shared devices but frustrating for enterprise users on managed laptops.

Conclusion

The default PHP session timeout of 2 hours is a hardcoded security feature designed to mitigate risks associated with stolen session tokens. Developers must explicitly configure session.gc_maxlifetime and coordinate it with session.cookie_lifetime to achieve the desired balance between security and usability. Ignoring these settings assumes the user will always close their browser, which is rarely the case in modern workflows.

Our network