How To Work with Session Timeout in Laravel 8

Share this Article
Reading Time: 4 minutes
4,054 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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.

Find More on Laravel 8 Articles here