Reputation: 1148
Is there a way in Laravel 5.7 to find out how much time a user has left before their current session expires?
EDITED
I also want to check when the current session expires for guests as well as logged in users.
Also, in the current answers, there seem to be conflicting ideas that a session will expire exactly "SESSION_LIFETIME" minutes after login, while others are thinking that the session expires after the last user activity. It would be good to clarify which is correct. Then also how to then get the last activity time and what classifies as activity on the session.
Also, I think Session::activity() has been removed in Laravel 5.7.
POSSIBLE SOLUTION
Thanks to @bharat-geleda below, this is a working solution if you are using Laravel 5.7 and storing sessions within the DB.
$last_activity = \DB::table('sessions')->select('last_activity')->where('id', session()->getId())->first()->last_activity;
$session_time_remaining_minutes = ($last_activity + (\Config::get('session.lifetime') * 60) - time()) / 60;
dd($session_time_remaining_minutes);
I'm currently unclear when 'last_activity' gets updated though, it isn't happening on page refresh.
Upvotes: 5
Views: 6217
Reputation: 31
So I know that this is an old one but I thought I'd share the solution I've come up with. I've implemented a middleware that updates the session variable "session_expiry" to be the current time plus the session.lifetime on the outbound phase of each request (after the request is processed). Throughout processing the request you can therefore check this session variable to check how much time is left. Below is a fragment from the middleware handle() method (you will see I only apply this to authenticated sessions):
// Return trip middleware
$response = $next($request);
// Update session expiry after request has been processed
if (auth()->check())
{
$expiry = (new \Carbon\Carbon())->addMinutes(config('session.lifetime'));
session()->put('session_expiry', $expiry);
}
elseif (session()->has('session_expiry'))
{
session()->forget('session_expiry');
}
return $response;
This updates with all requests, including AJAX and API requests. I've then presented a route for an ajax call to check if the session is still valid. With a timer that is set for 1 minute longer than the lifetime it enables the application to log out gracefully if the session has expired but remain active if other tabs have kept it alive. This timer is reset automatically after every AJAX call with the $(document).ajaxComplete method.
Upvotes: 2
Reputation: 3318
Add two columns in your authentication table(users) for example session_start_time and session_end_time
now when user login update current timestamp in session_start_time and add minutes according to your config/sessions.php (SESSION_LIFETIME);
so from differnce of current time and session_end_time you can get remaining time.
you can use carbon for timestamp and for difference
Upvotes: 2
Reputation: 11
If you would know when the user logged in, storing it in database users table, and you would know from config/sessions.php how long your session is before it expires, putting two together will allow you to compute how long it is before the users session expire.
Upvotes: 1