Automatic Logout after 15 minutes of inactive in php

Automatic Logout after 15 minutes of inactive in php

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

Stefan Bogdanescu

Founder & Senior Architect

Automatic Logout after 15 minutes of inactive in php

Implementing an automatic logout after a specific period of user inactivity is a standard security requirement, but it often leads to confusion about how PHP sessions actually work. The core mechanism relies on distinguishing between the "last activity timestamp" stored in the session and the raw $_SESSION superglobal data. When a user opens their browser, PHP initializes a session file and stores a variable (often named $last_activity) containing the current Unix timestamp. This value gets updated every time the application writes to the session file, whether that's loading a new page or updating a database record.

To enforce a 15-minute timeout, you must check this timestamp against the current system time on every request. If the difference exceeds 900 seconds (15 minutes), the session is considered expired. The standard approach involves modifying your session configuration in php.ini to ensure that sessions are only destroyed when they exceed this limit, rather than relying solely on application logic to check every single page view.

In a typical Laravel-style setup or vanilla PHP framework, you might configure the gc_probability and gc_divisor settings to control garbage collection, but setting a custom timeout usually requires adjusting the session.gc_maxlifetime. However, simply changing this value can be risky because it affects all sessions globally. A more granular approach involves checking the inactivity timer within your application code before processing any sensitive logic or rendering views.

Consider a middleware pattern for frameworks like Laravel, or a simple filter function for vanilla PHP. You would define a constant for the timeout duration:

define('INACTIVE_TIMEOUT', 900); // 15 minutes in seconds

Then, you retrieve the stored timestamp and compare it against time():

$session = $_SESSION;
$last_activity = isset($session['last_activity']) ? (int)$session['last_activity'] : 0;

if (time() - $last_activity > INACTIVE_TIMEOUT) {
    // Destroy session and redirect to index
    session_destroy();
    header('Location: /');
    exit;
}

This logic must be placed early in your request lifecycle, ideally before any output is sent. If you are using a framework like Laravel, this check is often integrated into the kernel.php bootstrapping process or via a dedicated middleware class that runs on every HTTP request. The key here is ensuring that the timestamp updates dynamically. You must write to the session storage with $session['last_activity'] = time(); whenever the user interacts with the page. Without this update, the session will age based on server time even if the user is actively typing or clicking, leading to unexpected logouts.

A common pitfall involves relying solely on session_destroy() without verifying the reason for expiration. If you set a custom garbage collection lifetime in php.ini, the PHP engine handles cleanup automatically, but this process is probabilistic and might not clean up exactly at the 15-minute mark unless configured precisely. For instance, setting session.gc_maxlifetime = 900 tells PHP to consider sessions older than 15 minutes as garbage during its next collection cycle. However, relying exclusively on this can lead to a race condition where a user has seconds of activity left before the session is wiped out by the daemon process. Therefore, application-level checks are preferred for strict security policies.

Another consideration is the storage driver. If you are using file-based sessions (the default), the timestamp is stored in the sess_ files on the server's disk. If you switch to database-backed sessions, the logic remains identical; you simply query the sessions table or read from the superglobal which Laravel populates automatically. The implementation details change slightly depending on whether you are managing the session array manually or using a framework helper like session()->flash(), but the arithmetic comparison remains constant.

If you need to handle this in a specific project context, such as one hosted by https://laravelcompany.com, you can leverage built-in helpers to streamline this check. For example, using Auth::check() combined with custom middleware allows you to offload this logic from controllers, keeping your routes clean and your security layers consistent. Always remember that redirecting to the index page should happen immediately after destroying the session to prevent any partial rendering or sensitive data leakage in the response body.

Our network