Constantly checking for activity in a page then log out if no activity function executeQuery() {
  $.ajax({
    url: '../include/activity.php',
    success: function(data) {
      // do something with the return value here if you like
      if(data.error == true){
           window.location.href = "login.php";
      }

    }
  });
  setTimeout(executeQuery, 3000); // you could choose not to continue on failure...
}

$(document).ready(function() {
  // run the first time; all subsequent calls will take care of themselves
  setTimeout(executeQuery, 3000);
});


In activity.php

if (isset($_SESSION['LAST_ACTIVITY']) &amp;&amp; (time() - $_SESSION['LAST_ACTIVITY'] &gt; 300)) {
    // last request was more than 5 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time
    session_destroy();   // destroy session data in storage
echo json_encode(array(
    'error' =&gt; true
));    
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp


if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
    echo json_encode(array(
        'error' =&gt; false
    ));    
} else if (time() - $_SESSION['CREATED'] &gt; 300) {
    // session started more than 5 minutes ago
    session_regenerate_id(true);    // change session ID for the current session an invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
    echo json_encode(array(
        'error' =&gt; false
    ));    
}


This is how I am checking for activity of user using ajax and php.
I have doubts if this is acceptable in real life environment.
I would like to have suggestion on how to implement this cleanly , correctly easily.
I would route all ajax calls through 1 function in JavaScript, and that function then keeps track of the last call. In essence, the browser itself should know whether there was activity or not.

If for some odd reason you need multiple windows with the same session to stay open as long as one of them has activity, then I would change the timer from 3000 to 60000. Checking once a minute should be more than fine.

Finally executeQuery is a terrible name, perhaps checkTimeOut?