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.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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