0

I am developing a project management system. I am trying to track working time of a user. The time tracking page contains a timer and the user start tracking before they start their work. Its working perfectly but in the case of system shut down or idle the time tracker doesn't stop. How can I check the system (Not the browser window or tab) is idle or not?And I want to show a confirmation message if browser tab is closed.I am use the code

    $( [window, document] .on( 'beforeunload ', function () {
        //alert
    });

But "beforeunload" can't return any value.How to return a true or false value in beforeunload function in javascript

Arturs
  • 1,258
  • 5
  • 21
  • 28
Navaneetha Nair
  • 312
  • 2
  • 12
  • possible duplicate of [How to know browser idle time?](http://stackoverflow.com/questions/9564602/how-to-know-browser-idle-time) – Anil Aug 21 '13 at 10:53
  • 2
    Access to system information from a web page is very restricted for security reasons. So I would say you can't (maybe there is some Flash hack, I don't know). I would suggest detecting window/tab close is your best option (although I hear it is not fail safe), or maybe a javascript interval that keeps pinging the server to say "Im still here", then you server can monitor for users that stop "pinging" – musefan Aug 21 '13 at 10:55
  • 1
    Spying on users like this is a _bad_ thing to do. – arkascha Aug 21 '13 at 10:59

1 Answers1

1

Tracking system would not be a safer side so detecting window/tab close is your best option.

Set a 'time' flag as session data, and check if the session is still 'new/not exceeding a certian limit' to keep them logged in(or your functionality) each pageload.

//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();

Another Option:

<?php

/* set the cache limiter to 'private' */
session_cache_limiter('private');

$cache_limiter = session_cache_limiter();

/* set the cache expire to 30 minutes */
session_cache_expire(30);

$cache_expire = session_cache_expire();

/* start the session */
session_start();

echo "The cache limiter is now set to $cache_limiter<br />";
echo "The cached session pages expire after $cache_expire minutes";

?>

Reference: session-cache-expire

softvar
  • 17,917
  • 12
  • 55
  • 76