There are several ways to do this. Here is a couple ...
set the session expiration time, so that after a certain time the session expires and is no longer valid.
set the “time” flag as the session data and check if their entire session is still “new enough” so that they are registered in each folder.
I would choose the second option, as it can be difficult to set the correct values in PHP so that the session is safe and the way you want. With the second option, all you have to do is make sure that the session does not expire before you want, which is easier.
Sample code for option 2:
//on pageload session_start(); $idletime=60;//after 60 seconds the user gets logged out if (time()-$_SESSION['timestamp']>$idletime){ session_destroy(); session_unset(); }else{ $_SESSION['timestamp']=time(); } //on session creation $_SESSION['timestamp']=time();
EDIT:
Your comment explains that you really want to track mouse events and other things on the client side to determine if the user is free. This is harder. I will give a general solution, and then offer a couple of suggestions for optimization and improvement.
To accomplish what you have described, you must track client activity (mouse movements, keyboard strokes, etc.) and process this information on the server side .
Tracking client activity will require javascript event handlers. You must create an event handler for each action that you want to consider “not idle,” and track (in the javascript variable) the last time they were in standby mode.
To process this information on the server side, you will need to use ajax to send the last time they were idle on the server. Therefore, every few seconds you should send an update to the server (using javascript), which indicates how long the user has been idle.
A few additional tips:
You should not rely on this ajax solution as the only way to track user activity, as some users do not have JS. Thus, you should also track user activity in pageload. Accordingly, you probably shouldn't set too little downtime (at least for non-JS users), since non-JS users will not send any data to the server until pageloads appear.
You should send updates to the server via ajax as little as possible regarding user activity to reduce server demand. To do this, just check javascript when the user will time out. If he gets to the point where the user is going to time out (say, after about a minute or so), then and only then the ping server.
source share