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.
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.
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 .
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.
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.
- Try if user exists in database
- If yes, and he is user who needs longer session lifetime, run
config(['session.lifetime' => $newLifetime]);
- Log user in
- Enjoy longer session lifetime for current user
— Source
You have to make the above changes in LoginController
.
change the SESSION_LIFETIME= (duration in minutes) @ env
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With