Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BobbyAxe61's avatar

Get Session Start Time And User Inactivity Period

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.

0 likes
7 replies
BobbyAxe61's avatar

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?

mushood's avatar

What does user inactivity mean for your project? Page open but not doing anything or did not do any valuable action on the site?

Also why use database to store your sessions? Any specific blocking issue?

BobbyAxe61's avatar

@mushood no am not using database to store sessions, user in activity for my project would mean how long since last request.

mushood's avatar

@bobbyaxe61 Post your answer.

Someone else might have this issue and will find this when googling ;)

Mark it as solved as well

1 like
BobbyAxe61's avatar
BobbyAxe61
OP
Best Answer
Level 1

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.

1 like

Please or to participate in this conversation.