How to Set or Increase Session Lifetime in Laravel

Laravel set or increase session lifetime example; In this tutorial, you will learn how to set the session timeout or session lifetime in your laravel web applications.

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

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:

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. After that, open .env file and set session timeout like following:

.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

In the first way, you saw that the session timeout or session lifetime was first set in the .env file and then it was gated from the .env file, then set the session timeout in the session.php.

In this step, you will learn how to set session timeout or session lifetime in config/session.php file.

So open your config/session.php file and set session timeout like following:

<?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 *