How To Increase Session Lifetime In Laravel


In this tutorial, we will guide you on how to increase the session timeout in Laravel. Specifically, we will show you how to set and increase the session lifetime in Laravel 6, Laravel 7, Laravel 8, Laravel 9 and Laravel 10.

Please note that while we cannot set a session lifetime permanently, we can specify it in minutes to determine when sessions should expire. In this example, we will set a session expiration time of 1 year

60 * 24 * 365 = 525600 // 1 year

If you want to increase your session lifetime then we need to change in .env file and it is very easy to change it from the configuration file in laravel. laravel provides you session.php file there is we can see 'lifetime' key option for setting time in minutes.

In the session configuration file there a also several options for setting expire_on_close, encrypt, timeout driver, etc.

 

Here I will give you a solution for increasing session timeout in laravel.

Example 1: Using .env File

We need to define value in minutes in your .env file as below:

.env

SESSION_LIFETIME=525600

 config/session.php

<?php
  
use Illuminate\Support\Str;
  
return [

    'lifetime' => env('SESSION_LIFETIME', 120),
  
]

 

Example 2: Using Config File

config/session.php

<?php
  
use Illuminate\Support\Str;
  
return [
    
    'lifetime' => 1 * (60 * 24 * 365),
  
]

In some conditions also happens cache issue, So, we need to clear it.

For clearing Cache, View, and Routes in Laravel check below.

And, Now your session timeout time will be increased...

 


You may also like:

RECOMMENDED POSTS

FEATURE POSTS