Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel session expire time for each session

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.

like image 300
Nasser Ali Karimi Avatar asked Feb 19 '18 04:02

Nasser Ali Karimi


People also ask

How long does session last Laravel?

How long does laravel session last? You need to understand what happened: You've set the lifetime of the sessions to 120 minutes, which means after 120 minutes the session is flushed. The remember_me feature is using cookies.

How do I reduce session lifetime in Laravel?

Set Session Lifetime in ENV File Ideally, laravel doesn't allow you to increase session expiration time forever; nevertheless, you may set the session expiration time for several minutes or for the next one year. You can now take the total minutes, append these minutes in the .

How do you get session lifetime in Laravel?

If you want to increase your session life time then you can easily do it from configuration file in laravel. laravel provide session. php there is a 'lifetime' key option for setting time in minutes. in session configuration file there is a also several option for set driver, timeout, expire_on_close and encrypt etc.


2 Answers

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.

like image 150
Jomoos Avatar answered Oct 21 '22 00:10

Jomoos


change the SESSION_LIFETIME= (duration in minutes) @ env

like image 27
Mohammed Shabeer k Avatar answered Oct 21 '22 01:10

Mohammed Shabeer k