skip to Main Content

Good morning,
I am looking for an identifier when a user connected but inactive for 30 minutes will be closed by the Plesk server
(30 minutes of inactivity is my server setting before closing the session)

I have an application developed in PHP hosted and administered under plesk.
since plesk I determine the time of inactivity before closing a session.
when a user leaves his page open without activity for more than 30 minutes, his session is automatically closed but without warning or message.
so to prevent him from believing that the application is no longer working I would like to be able to display a message to him indicating that the session has expired, but I am unable to do so

Thank you for your help.

inside a function called every minute I do the following test:

if (session_status() === PHP_SESSION_DISABLED || session_status() === PHP_SESSION_NONE ) {
  respAjax::errorJSON(array('message' => ($usrActif->lang == 'FR' || $usrActif->lang == '' ? 'SESSION EXPIREE' : 'SESSION EXPIRED')));
}

1

Answers


  1. You can start timer when user logs in

    let inactivityTimer = setTimeout(function() {
      // User has been inactive for 30 minutes, display message
      alert('Your session has expired due to inactivity.');
    }, 30 * 60 * 1000); // 30 minutes in milliseconds
    

    then you can reset timer when any activity happens

    // Reset the timer when the user performs an action
    function resetTimer() {
      clearTimeout(inactivityTimer);
      inactivityTimer = setTimeout(function() {
        alert('Your session has expired due to inactivity.');
      }, 30 * 60 * 1000);
    }
    
    // Bind resetTimer to various events that indicate user activity
    document.addEventListener('mousemove', resetTimer);
    document.addEventListener('keydown', resetTimer);
    document.addEventListener('scroll', resetTimer);
    // etc.
    

    also u can clear timer if you want when user logout

    // Clear the timer when the user logs out
    function logout() {
      clearTimeout(inactivityTimer);
      // Do any other logout logic
    }
    

    Note : this has nothing to do with ur php this is all done by javascript

    Just in addition this might help someone :

    Another thing if you dont want session to expire whole day or till browser is closed then you can just make a simple jquery call via ajax to server every 5-10 minutes (accordingly) and it will never expires. You can also update values if needed in client end with this ajax request.

    Here is example :

    function keepSessionAlive() {
        $.ajax({
            url: "keepSessionAlive.php",
            success: function() {
                // Session is still alive
            },
            error: function() {
                // Session has expired or there was an error
            }
        });
    }
    
    setInterval(keepSessionAlive, 5 * 60 * 1000); // 5 minutes in milliseconds
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search