How would i get when a session was started for a particular user and also get how long since the user was last active (since user last loaded a page on the site) for laravel 6.X
I cant seem to find this in the documentations and numerous discussion, would be glad if someone could point me in the right direction.
Thanks @mushood, i understand that i could do this manually and override a lot of perfectly working controllers :) but i was hoping for a laravel way to do this, there is no need to reinvent the wheel.
if there is no other way then i will do this manually, i was convinced this could be done due to Session::getMetadataBag(); in older laravel versions but seems that session start time is only possible if your using database to store your sessions.
what about the user inactivity? how would you go about implementing it if you were to do it manually?
I solved this problem by creating a middleware that runs on every web request.
// Declare a variable
$user_active = false;
// Check the users last request time
if ($last_request_time = $request->session()->get('last_request_time')){
if ($last_request_time + 3600 > time()) {
$user_active = true;
$request->session()->put('last_request_time', time());
}
} else {
// if no request time assume user is from login process
$request->session()->put('last_request_time', time());
}
The code above checks if the user has made a request in the past one hour hence the number 3600secs. if user has made a request in the past one hour the user request time is updated. if not the $user_active will remain false. else if the user has no last_request_time key in his session assume user is from login process in and update user request time.