如何在php中检查系统是否空闲


How to check system is idle or not in php?

我正在开发一个项目管理系统。我正在尝试跟踪用户的工作时间。时间跟踪页面包含一个计时器,用户在开始工作之前开始跟踪。它工作得很好,但在系统关闭或空闲的情况下,时间跟踪器不会停止。如何检查系统(不是浏览器窗口或选项卡)是否空闲?如果浏览器选项卡关闭,我想显示一条确认消息。我正在使用代码

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

但是"beforeunload"不能返回任何值。如何在javascript 中的beforeunload函数中返回true或false值

跟踪系统不会是一个更安全的方面,因此检测窗口/选项卡关闭是您的最佳选择。

设置一个"时间"标志作为会话数据,并检查会话是否仍然是"新的/不超过一定的限制",以使他们在每次页面加载时都能登录(或您的功能)。

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

另一种选择:

<?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";
?>

参考:会话缓存过期