PHP Javascript Logout User After Inactivity

PHP Javascript Code Logout User After X Number Minutes Inactivity with Page Refresh

Home Short:

There is a lot of partial code online on how to use php or php and javascript to log a user out after inactivity. Most of the code is very basic and incomplete. For exampe code that will log the user out after x minutes of inactivity but not providing feedback of the logout.

The code below uses both Javascript and PHP to log a user out after x number of minutes inactivity which means ...

Any regular code that utilizes a timer in Javascript is ineffective because when a browser tab loses focus, the browser throttles activity in that tab which causes the actual countdown of a timer via setInterval or setTimeout to slow down so the javascript and/or php code to log the user out is delayed beyond what is intented.

While this code may seem fairly lengthy, it works perfectly and is immune to innactive browser tab throttling. Also, this works on all tabs if there are multiple tabs to your website open.

The following Javascript and PHP code to log a user out after x number of minutes inactivity ...

At the very top of your php pages should be in a separate php file ie session-start.php

In your Javascript code at the bottom of the page(s) you want auto logged out of after x number minutes inactivity ...

The following code goes in your path-to-thread.js file

In your url-to-php-logout.php file ...

S
H
A
R
E