Laravel 5 Session Lifetime

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.

  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.

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



Leave a reply



Submit