是有可能发现在PHP闲置使用类似甚至鼠标焦点在JavaScript中检测

问题描述:

但是有很多的PHP的方式来退出应用程序时,用户使用会话处于闲置状态,我使用是有可能发现在PHP闲置使用类似甚至鼠标焦点在JavaScript中检测

同时登录

$_SESSION['last_activity']=time()+10; 

在头

$expire_time = 10; //10 secs 
if($_SESSION['last_activity'] < time()-$expire_time) { 
    echo 'session destroyed'; 
} 
else { 
    $_SESSION['last_activity'] = time(); 
} 

此功能将注销用户根据用户点击或刷新页面甚至在标签,但不鼠标事件这是有可能在JavaScript

var IDLE_TIMEOUT = 900; //seconds 
var _idleSecondsCounter = 0; 

document.onclick = function() { 
    _idleSecondsCounter = 0; 
}; 

document.onmousemove = function() { 
    _idleSecondsCounter = 0; 
}; 

document.onkeypress = function() { 
    _idleSecondsCounter = 0; 
}; 

window.setInterval(CheckIdleTime, 1000); 

function CheckIdleTime() { 
    _idleSecondsCounter++; 
    var oPanel = document.getElementById("SecondsUntilExpire"); 
    if (oPanel) 
     oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + ""; 
    if (_idleSecondsCounter >= IDLE_TIMEOUT) { 
     alert('Times up!, You are idle for about 15 minutes, Please login to continue'); 
     document.location.href = "logout.php"; 
    } 
} 

而且在整个项目或制表符这不会工作,如果用户保持一个标签空闲,并在另一个标签上工作,整个项目将被注销,有没有什么办法让这个脚本全局工作或使PHP检测所有事件。

+0

尝试使用cookie设置时间,然后使用javascript检查 – Edwin

+0

? –

+0

负面选民你能否解释沃茨这个问题的错误? –

您可以使用JavaScript的本地存储API

具有本地存储,Web应用程序可以在用户的​​浏览器本地存储数据。

在HTML5之前,应用程序数据必须存储在cookie中,并包含在每个服务器请求中。本地存储更安全,大量数据可以存储在本地,而不会影响网站性能。

与Cookie不同,存储限制要大得多(至少5MB),并且信息永远不会传输到服务器。

本地存储是每个来源(每个域和协议)。来自一个来源的所有页面可以存储和访问相同的数据。

var IDLE_TIMEOUT = 900; //seconds 
    sessionStorage.idleSecondCounter = 0; 

document.onclick = function() { 
    sessionStorage.idleSecondCounter = 0; 
}; 

document.onmousemove = function() { 
    sessionStorage.idleSecondCounter = 0; 
}; 

document.onkeypress = function() { 
    sessionStorage.idleSecondCounter = 0; 
}; 

window.setInterval(CheckIdleTime, 1000); 

function CheckIdleTime() { 
    sessionStorage.idleSecondCounter = parseInt(sessionStorage.idleSecondCounter)+1; 
    var oPanel = document.getElementById("SecondsUntilExpire"); 
    if (oPanel) 
     oPanel.innerHTML = (IDLE_TIMEOUT - sessionStorage.idleSecondCounter) + ""; 
    if (sessionStorage.idleSecondCounter >= IDLE_TIMEOUT) { 
     alert('Times up!, You are idle for about 15 minutes, Please login to continue'); 
     document.location.href = "logout.php"; 
    } 
} 

的的sessionStorage对象等于localStorage的对象,不同之处在于它存储的数据只有一个会话。当用户关闭特定浏览器选项卡时,数据将被删除。

+0

这也不是跨标签: –

+0

尝试'localStorage.setItem('idleSecondCounter',0);'而不是'sessionStorage.idleSecondCounter' –

+0

和PLZ告诉我哪个浏览器是你使用 –