Laravel 5 Session Lifetime

Laravel 5 Session Lifetime

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding Laravel Session Lifetime Configuration

A common point of confusion arises when developers inspect config/session.php and encounter a 'lifetime' setting of 120, leading to the assumption that sessions expire after two minutes. This interpretation is incorrect. In the context of the Laravel configuration file, this value represents minutes, not seconds. If your users are logging out prematurely, checking this specific line will not be the culprit unless you have explicitly modified the default configuration or deployed a custom version where this unit was misinterpreted.

The default setting of 120 minutes translates to two hours of idle time before the session data is considered expired on the server side. However, even with this generous window, users often experience unexpected logouts. This discrepancy usually stems from how the 'expire_on_close' directive interacts with browser behavior and cookie policies, rather than a typo in the lifetime value itself.

The configuration snippet provided shows both lifetime set to 120 and expire_on_close set to true. When expire_on_close is enabled, Laravel instructs the application to set the session cookie's expiration time to the past as soon as the browser window or tab is closed. This forces the browser to immediately discard any existing session tokens associated with that specific domain. Consequently, even if the server-side session storage has valid data for another 119 minutes, the client will no longer be able to present a valid cookie upon reopening the browser. To the application logic, this looks exactly like a logout event because the authentication token is missing.

To diagnose whether expire_on_close is causing premature logouts, inspect your session cookie settings in the developer tools of your browser. Navigate to the Application or Storage tab, locate the cookies for your domain, and check the Expires attribute. If this value is a timestamp from the past, it confirms that the expire_on_close directive is active.

If you intend for users to remain logged in across different browsing sessions or after closing the browser, you must adjust this configuration. You can change the 'expire_on_close' setting to false. When disabled, the session cookie will behave like a standard persistent cookie, respecting the lifetime duration regardless of whether the browser window is closed.

// config/session.php
'lifetime' => 120,
'expire_on_close' => false, // Change from true to false
'expired_session_redirect' => url(env('APP_URL'))

Setting 'expire_on_close' to false is particularly important for production environments where users might switch between tabs or close their browsers during a work session. Without this adjustment, the application assumes the user has intentionally logged out every time they close the window, which can lead to frustration and broken workflows.

There are also other factors that can mimic session expiration. For instance, if your application is running behind a load balancer with multiple servers, ensure that session storage is centralized (such as using Redis or a database) rather than relying on PHP's default file-based storage. If sessions are stored locally, a user might request a page from Server B while their session data exists only on Server A, resulting in an authentication failure that feels like a timeout.

Additionally, verify your server's session.gc_maxlifetime setting in php.ini. Laravel respects this PHP-level garbage collection limit as well. If the PHP configuration sets session.gc_maxlifetime to a lower value than your .env file specifies in the session driver, the server will still clean up old session files based on the stricter of the two settings.

# Check php.ini or .user.ini
session.gc_maxlifetime = 7200

In summary, the 120 value in config/session.php is definitely in minutes. If your users are logging out immediately upon closing the browser, the issue lies with the expire_on_close flag being set to true. Disabling this flag ensures that the session persists for the full 120-minute window defined by the lifetime configuration, providing a smoother user experience without requiring manual intervention from the application.

Our network