How To Set Time Limit For Session In Laravel 9

In this article, we will see how to set a time limit for session in laravel 9. Here, we will learn to increase session lifetime in laravel 8 and laravel 9. We can not set session lifetime permanently but we can set in minutes for the session expiration time. So, here we will set 1 year time for the session to expire.

So, let's see session timeout in laravel 9, how to increase session timeout in laravel 8/9, laravel 9 set session expiration time, and laravel 9 session lifetime.

60 * 24 * 365 = 525600 // 1 year

If you want to increase your session lifetime then we need to change it in the .env file and it is very easy to change it from the configuration file in laravel 8 and laravel 9.

Laravel provides you session.php file there is we can see the 'lifetime' key option for setting time in minutes.

In the session configuration file there is an also several options for set expire_on_close, encrypt, timeout and driver, etc.

Here, we will see a solution for increasing session timeout in laravel 9.

Example: Using .env File

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

.env

SESSION_LIFETIME=525600

 config/session.php

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

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

 

Example: Using Config File

config/session.php

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

Sometimes, happen cache issues. So, we need to clear it.

For clearing Cache, View, and Routes in laravel check the below link.

And, now your session timeout time will be increased.

 


You may also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS