Laravel session expire time for each session

32,794

Solution 1

You can change the session lifetime, application wide by changing the lifetime value on config/session.php:

/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. 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.

  1. Try if user exists in database
  2. If yes, and he is user who needs longer session lifetime, run config(['session.lifetime' => $newLifetime]);
  3. Log user in
  4. Enjoy longer session lifetime for current user

Source

You have to make the above changes in LoginController.

Solution 2

change the SESSION_LIFETIME= (duration in minutes) @ env

Share:
32,794
Nasser Ali Karimi
Author by

Nasser Ali Karimi

PHP & JavaScript Developer since 2016

Updated on February 02, 2022

Comments

  • Nasser Ali Karimi
    Nasser Ali Karimi over 2 years

    Can we set expire time for each session in Laravel?

    If we can how it possible from the controller by each session that we create? Thanks.

  • Admin
    Admin about 6 years
    is that per user? or the whole site?
  • Jomoos
    Jomoos about 6 years
    @rtfm If we change it in the configuration file, it will be for the whole site. If we change it in LoginController as outlined in the above 4 steps, it will be per user.