我想实现一件事。我用多个标签打开我的网站。当我在一个选项卡中工作时,其他选项卡不应该超时。它应该是活的。如何为其他标签保持活力。我使用下面的js来查找空闲时间注销。
<script type="text/javascript">
idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval("timerIncrement()", 60000); // 1 minute
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
})
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 2) { // 30 minutes
var beforeTime = new Date()
var bminutes = beforeTime.getMinutes();
var bseconds = beforeTime.getSeconds();
var user='<?php echo Auth::getSessionUserFullName();?>';
if(user!='')
{
var timehours=beforeTime.getHours();
var timeoutmin=bminutes+1;
var timeoutseconds=bseconds-1;
if(timeoutseconds>59)
{
timeoutseconds=0;
timeoutmin=timeoutmin+1;
}
if(timeoutmin>59)
{
timeoutmin=0;
timehours=hours+1;
}
if(timehours>24)
{
timehours=0;
}
var ok=confirm("Your session expire time started at "+beforeTime.getHours()+":"+beforeTime.getMinutes()+":"+beforeTime.getSeconds()+".Please click 'OK' before "+timehours+":"+timeoutmin+":"+timeoutseconds+" to stay signed in.");
if(ok)
{
var currentTime = new Date()
var aminutes = currentTime.getMinutes();
var aseconds = currentTime.getSeconds();
var time=aminutes-bminutes;
if(aminutes>bminutes && aseconds>bseconds)
{
alert('Time out!!Please login again to access.');
window.location.href='<? echo APPLICATION_WEBROOT?>auth/logout';
return false;
}
else if(time>=2)
{
alert('Time out!!Please login again to access.');
window.location.href='<? echo APPLICATION_WEBROOT?>auth/logout';
return false;
}
else
{
return true;
}
}
else
{
window.location.href='<? echo APPLICATION_WEBROOT?>auth/logout';
}
}
}
}
</script>
请帮帮我。如何为所有打开的选项卡保持会话。提前感谢
会话不是特定于选项卡的。它适用于整个浏览器实例。
当用户向服务器发出请求时,会话被扩展。因此,根据您的应用程序,您可能必须向服务器生成常规请求以扩展该会话。
为此,您可以使用setInterval javascript函数触发对PHP后端的AJAX调用。希望对你有帮助。
BTW:要小心,因为有些浏览器会停用没有用户焦点的选项卡(例如Chrome) -它不会修改会话…但一些JS可能会被暂停。
我能想到的一种方法是在服务器中自己处理会话。
这是一个可能有效的系统(欢迎提出改进建议):
1)在服务器DB中存储您自己的自定义会话,IP作为唯一标识符。
2)每当用户与服务器交互时,获取其IP并更新服务器中存储的相应会话。如果该会话已经超时,将用户重定向到超时页面。
可能有很多事情我没有考虑过,但在我看来,这应该是一种可行的方式来维护跨选项卡会话