Session expire time for each session

Session expire time for each session

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

Stefan Bogdanescu

Founder & Senior Architect

Granular Session Expiration in Laravel

Laravel's session management is robust by default, offering a single SESSION_LIFETIME configuration value that applies globally to every user. This approach simplifies development but creates friction when you need different expiration times for different parts of your application. For instance, a guest checkout flow might only need to persist for 15 minutes, while a logged-in dashboard session could require days of inactivity before invalidation. The core question is whether Laravel supports per-session timeout configuration directly from the controller.

The short answer is that you cannot simply pass a duration parameter to session()->put() or redirect(). The underlying session driver (file, database, redis) and the kernel's startSession middleware rely on the global lifetime setting defined in config/session.php. When the kernel initializes the session, it reads this single configuration value and sets the LAST_ACTIVITY timestamp for that specific session. Subsequent activity updates this timestamp, effectively extending the session until it exceeds the global limit plus the grace period configured for garbage collection.

To achieve granular expiration, you must manipulate the session's underlying storage or bypass the standard lifetime logic entirely. The most direct method involves modifying the LAST_ACTIVITY field in the session driver directly. Since Laravel stores sessions as JSON (for file and database drivers) or hash strings (for Redis), you can access the raw session data and inject a custom expiration timestamp.

Here is how you might implement this within a controller action:

use Illuminate\Support\Facades\Session;

public function quickAction()
{
    $seconds = 900; // 15 minutes in seconds
    
    // Get current session instance
    $session = Session::get();
    
    // If using file or database driver, we can access the underlying store
    $store = $session->getStoreInstance();
    
    // Retrieve raw session data
    $data = $store->all();
    
    // Calculate absolute expiration time (now + seconds)
    $absoluteExpiration = time() + $seconds;
    
    // Store custom expiration key
    $data['_custom_expire'] = $absoluteExpiration;
    
    // Update the store
    $store->put($data);
    
    return view('quick-action');
}

This approach relies on checking the _custom_expire key in your session middleware or a global filter. However, this method has significant drawbacks. It requires custom middleware to constantly check this key and potentially invalidate the session before the global SESSION_LIFETIME expires. Furthermore, if you switch drivers (e.g., from file to Redis), the structure of the stored data changes slightly, requiring code adjustments to ensure consistency.

A more elegant, albeit slightly more complex, solution involves overriding the session lifetime specifically for that request using the session()->flush() method combined with a new SESSION_LIFETIME scope if your application supports it, or by utilizing Laravel's built-in SessionTimeout logic via events. However, Laravel does not natively support setting a unique TTL per session object without custom logic.

The most maintainable approach for complex requirements is to separate the concerns. Use global sessions for persistent data and store short-lived tokens in cookies with specific expiration headers or use Redis with individual key expiration (TTL). For example:

php artisan tinker
>>> Session::put('data', 'value');
>>> $session = new Illuminate\Session\Store($app, 'file'); // Specific instance
// Or utilize Redis with explicit TTL
>>> Redis::set('user:123:token', 'val', ['expire' => 900]);

If you must keep everything in the Laravel session store, consider creating a dedicated middleware that runs at the end of the request lifecycle. This middleware would inspect specific session keys (like _quick_token) and compare their stored absolute expiration time against the current server time. If the key is older than the current timestamp, the middleware deletes the session data or redirects the user to login immediately.

This architectural shift moves the responsibility from configuration to logic. It allows you to define quick_token with a 15-minute TTL and dashboard_token with a 7-day TTL within the same global session store. The trade-off is increased complexity in your middleware stack and potential performance overhead if checking every request against multiple expiration keys.

Ultimately, while Laravel provides a unified interface for sessions, true per-session expiration requires either manipulating the underlying driver's data structure or implementing custom validation logic. Choose the approach that best fits your security requirements and the volume of concurrent users you expect to handle.

Our network