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

johnw65's avatar

Session Timeout counter

I know this idea of somewhat flawed as discussed here: https://laracasts.com/discuss/channels/laravel/get-session-timeleft, but my client want the session timeout counter.

The session is set to 10 minutes. Now 1 minutes prior to the session timing out, I want to give an user an alert message and if there is no user response, then it will redirect to login screen after few seconds. The redirect to the login screen is working, but what is considered no activity/no response (no request?). What is the best way to detect this so the counter can be reset. I have the following code, and it's resetting the the session timer although the session has timeout already.


var inactiveTime;
var alertWinOpen = false;


  $('*').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
	pathname = $(location).attr('pathname');
	if(pathname=='/login'){
  	return;
	}

	function alertUserSession() {
  	if(alertWinOpen===false){
    	    OpenWindow();
  	}
	}

	clearTimeout(inactiveTime);
	inactiveTime = setTimeout(alertUserSession, 1000 * 600); // 10 minutes

  	
  });
0 likes
4 replies
LaryAI's avatar
Level 58

The code you provided is using JavaScript to track user activity and display an alert message 1 minute before the session times out. However, it seems that the session timer is being reset even after the session has already timed out.

To fix this issue, you can modify the code to check if the session has already timed out before resetting the timer. Here's an updated version of the code:

var alertWinOpen = false;
var sessionTimeout = 10 * 60 * 1000; // 10 minutes in milliseconds
var inactiveTime;

function alertUserSession() {
    if (alertWinOpen === false) {
        OpenWindow();
    }
}

function resetSessionTimer() {
    clearTimeout(inactiveTime);
    inactiveTime = setTimeout(alertUserSession, sessionTimeout);
}

$('*').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () {
    var pathname = $(location).attr('pathname');
    if (pathname == '/login') {
        return;
    }

    resetSessionTimer();
});

resetSessionTimer();

In this updated code, we define a sessionTimeout variable to store the session timeout duration in milliseconds. We also define a resetSessionTimer function that clears the previous timer and sets a new one.

The resetSessionTimer function is called both when the page loads and whenever there is user activity. This ensures that the session timer is reset only if the session has not already timed out.

Please note that this solution assumes you have a function named OpenWindow that displays the alert message. You may need to define this function or modify the code accordingly.

Remember to test the code thoroughly to ensure it meets your requirements.

Snapey's avatar
Snapey
Best Answer
Level 122

No idea what Lary is talking about.

Session will timeout if there is no interaction with the server, nothing to do with moving the mouse etc.

You have not said if you have any ajax requests sent from the page, but it would need to be an ajax request or a full page reload to reset the session timer. If you are sending ajax requests, then part of that needs to reset your local timer.

johnw65's avatar

@Snapey , thank you very much. So I guess I need to modify the function to check for ajax or page reload. What is the best way to detect an ajax request and page reload so I can reset the timer?

Snapey's avatar

@johnw65 you don't need to. The page is reloaded. Any script you were running before is gone and swapped out for anything on the new page

Please or to participate in this conversation.