0

I've found this script that runs as a "TimeOut". The scripts works fine, but before that, i want a php script to check if user is Inactive from Browser..

Is there a chance to do it using php?

if ($_SESSION['timeleft'] + 10 * 60 < time()) {
    echo '<script> window.location = "?logout";</script>';
} 
Banana
  • 2,435
  • 7
  • 34
  • 60

1 Answers1

1

PHP isn't really built for that, you can use JavaScript and make AJAX calls to a PHP page updating it with user activity.

An accepted way to do what you're apparently trying (use a dynamic session which logs people out after X seconds of inactivity), is to send such an update on every call to the server. If your site is synchronous (i.e. not a one-page application), you can expect users to navigate to different pages so this code can be in the page header.

If you have a one-page application, or expect users to stay on the same page for a long time (nowadays quite common), you can send an AJAX request every X seconds that updates the server.

WordPress takes both approaches, for example.

Ynhockey
  • 3,845
  • 5
  • 33
  • 51