How To Work with Session Timeout in Laravel 8

Share this Article
Reading Time: 4 minutes
22,928 Views

In laravel 8 version we have several options available to work authentication like login, register etc. Packages available as Jetstream with Livewire, Inertia.js, Laravel UI, and Breeze authentication.

Learn More –

By default in laravel application session timeout value is 120 minutes. Session Timeout simply means Not Used Anymore. When system is idle then session will be destroyed automatically after the given time period.

Session timeout represents the event occurring when a user does not perform any action on a web site during an interval. Inside this article we will see How to Increase or decrease Session timeout in Laravel 8.

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Set Application Session Timeout

Open .env file from application root. You will see we have an environment variable available to set it’s timeout value.

SESSION_LIFETIME=120

This 120 is in minutes. The given line means if system is idle for 2 hours then session will be automatically destroyed.

Setting Session Timeout To Year

In an year, total minutes is equals to

60 * 24 * 365 = 525600

Update .env file with this new value.

SESSION_LIFETIME=525600

How Environment Variable Works

Each value of .env file i.e environment variables are accessible at any point of application by using global helper function env().

Open session.php from /config folder. Search for lifetime

You will see something like this,

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

Application uses this lifetime key inside application for session timeout.

env(‘SESSION_LIFETIME’, 120) env function is searching SESSION_LIFETIME variable inside .env file, if it exists then it’s value will be used to session timeout value else default 120 value which is second parameter in env() will be used.

If we pass direct value to session.php, also it works same what we have done with .env

# .env
SESSION_LIFETIME=525600

OR

# session.php
'lifetime' => 525600,

Additionally, if you want to destroy session on browser close simply set this value to true in session.php.

'expire_on_close' => true,

We hope this article helped you to learn How To Work with Session Timeout in Laravel 8 in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5