How to Increase Session Lifetime in Laravel

How to Increase Session Lifetime in Laravel

The session-timeout element defines the default session timeout interval for all sessions created in this web application. The specified timeout must be expressed in a whole number of minutes. If the timeout is 0 or less, the container ensures the default behaviour of sessions is never to time out

Laravel increase session lifetime; In this tutorial, you will learn how to increase session lifetime or timeout by setting it in your laravel web applications.

This laravel session tutorial will cover following topics:

  • How to Set or Increase Session Lifetime in Laravel
  • What is default session time in laravel?
  • How does laravel session expire?

Note that, you can not set session timeout for forever. But you can set easily session in your laravel web application.

For Example, if you want to set session timeout for 1 year in your laravel web application. You can do the following:

SESSION_LIFETIME = 60 * 24 * 365 = 525600

How to Set or Increase Session Lifetime in Laravel

Here, you will learn two ways to set session life or session timeout for your laravel web application.

1: – Using .env File

First of all, Go to your project root directory and find .env file. And open .env file and set session timeout like the following to increase session timeout or lifetime:

.env

SESSION_LIFETIME=525600

When you set the timeout of the session in the .env file. So the config folder has a session.php. In this file, the session timeout of your laravel web application is set in this way.

<?php
  
use Illuminate\Support\Str;
  
return [
  
    .....
  
    'lifetime' => env('SESSION_LIFETIME', 120),
  
    .....
  
] 

2: – Using Config File

Now, open config/session.php file there is a lifetime key option for setting time in minutes. Here is an example how to increase session lifetime or timeout using config/session.php file:

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

What is default session time in laravel?

cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means “until the browser is closed.” Defaults to 0.

How does laravel session expire?

If you want them | to immediately expire on the browser closing, set that option. | */ ‘lifetime’ => 4320, ‘expire_on_close’ => false, Now, if you want to control the session lifetime per user, you need to set this value before logging in the user.

Conclusion

In this post, you have learned two ways to set session timeout or session lifetime for laravel web applications.

Recommended Laravel Posts

Recommended:-Laravel Try Catch

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *