Laravel session expire time for each session
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
.
Laravel 5.1 Configuration Session Lifetime Apply Changes
php artisan config:cache
applies changes to the configuration files!
Laravel session timeout, extra logout code
You are comparing session lifetime same as in middleware.
That Means when session will expire, your middleware will not(never) called.And user will move to login page.
If you want to save entry in Database, You can set long-time session lifetime, and in middleware use your custom time to logout.
Change in config/session.php
'lifetime' => 525600, // for one year, it will be in minute, use as you want.
Change in middleware as below, log out after two hours.
if (now()->diffInMinutes(session('lastActivityTime')) >= (120) ) { // also you can this value in your config file and use here
if (auth()->check() && auth()->id() > 1) {
$user = auth()->user();
auth()->logout();
$user->update(['is_logged_in' => false]);
$this->reCacheAllUsersData();
session()->forget('lastActivityTime');
return redirect(route('users.login'));
}
}
By this way your session will not expire automatically and you can manipulate data.
Related Topics
Error Building:Fatal Error: Pcre.H: No Such File or Directory
Php's File_Exists() Will Not Work for Me
PHP How to Create Multiple Sessions
Difference Between File, File_Get_Contents, and Fopen in PHP
Any Way to Keep Curl's Cookies in Memory and Not on Disk
Fatal Error: Call to Undefined Function Mb_Strlen()
How to Include File Outside Document Root
Laravel 5.3 Withcount() Nested Relation
Windows Cmd.Exe "The System Cannot Find the Path Specified."
Sql_Calc_Found_Rows/Found_Rows() Does Not Work in PHP
Magento - Redirect Customer from Observer Method
Code to Parse User Agent String
JSON_Decode() Returns Null Issues