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

ivanharmat's avatar

Skip session refreshing on certain ajax request

I'm trying to check if user is inactive with Ajax request, every 30 seconds:

setTimeout(function(){
    check_if_user_logged_in();
}, 30000);

function check_if_user_logged_in() {
    $.ajax({
        url : '/check_if_user_logged_in',
        type : 'GET',
        dataType : 'json',
        success : function(data){
            // User not logged in
            if(!data.logged_in) {
                // Do something - redirect or show pop up window
            }
        }
    });
    
    // Call this function again every 30 seconds
    setTimeout(function(){
        check_if_user_logged_in();
    }, 30000);
}

A controller method :

public function check_if_user_logged_in(Request $request)
{
    if($request->ajax())
    {
        if(\Auth::check())
        {
            return json_encode(array('logged_in' => true));
        }
        else
        {
            return json_encode(array('logged_in' => false));
        }
    }
}

All this works fine, but each ajax request updates the session, so even if user is inactive, session will never expire. Is there a way to skip session update for only this ajax request? I'm not sure where the session refreshing happens. I found this middleware class - StartSession.php, but I wasn't sure if it's a good practice to mess with the system files. Thanks.

0 likes
0 replies

Please or to participate in this conversation.