0

I am developing a very front-end driven application based on php,mysql and jquery . This app also requires a User Tracking feature where in the admin can track if the users are currently online or not .

I am using the session expiry time to expire the session after 2 hours and most importantly i am checking if there is any activity on the application via jquery . Below is my code to check the idle time , after which the user will be logged out :

 var idleTime = 0;
$(document).ready(function () {

    var idleInterval = setInterval(timerIncrement, 60000); // 1 minute

    //detect mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });
});

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime >15) { // 15 minutes of inactivity
        window.location.href='logout.php';
    }
}

This works perfectly fine if the user has logged in but is idle .

The issue is when the users are not using the browsers . For instance , if a user logs in and closes the browser or the tab then i cannot check if the user is active or logged out or idle .

Would be great if anyone could share their expertise on this one .

Thanks in advance.

slim shady
  • 11
  • 4
  • 1
    You could use some kind of polling ajax, or method using websockets. For example every 60seconds send a request to the server indicating the client is still connected. If a connected client goes for longer than 2 minutes without reporting in, they've closed the tab/browser, maybe mark them as idle. If they go for longer than 15minutes then mark them as logged out. – SubjectCurio Mar 05 '15 at 09:11
  • or simply use server side (php) session and le tthe session garbage collection mechanism do it's work. of course you need to ajax requests to server as @MLeFevre mention. – Santa's helper Mar 05 '15 at 09:18
  • Thanks . But to even send a request i need the user to be logged in and using the browser , right ? What and where do i right the script where in i make sure the server is getting a response ? – slim shady Mar 05 '15 at 09:18
  • well its a little up to your service's flow, in my humble opinion if you have a logout.php then why dont you have a login.php? – Santa's helper Mar 05 '15 at 09:20
  • Of course i have login.php . What's login.php got to do with this ? I set the session inside login.php – slim shady Mar 05 '15 at 09:21
  • its not related directly but its for create a session on server side, so your JS code does some ajax requests like @MLeFevre mention and if you use `session_name()` `session_start()` functions, php will automaticly do the garbage collections make their session expire. please take a look at the Session section at the php.net – Santa's helper Mar 05 '15 at 09:30
  • You could use the window.onbeforeunload event. May check http://stackoverflow.com/questions/13443503/run-javascript-code-on-window-close – Greg Kelesidis Mar 05 '15 at 11:28

0 Answers0