即使我在同一浏览器中的多个选项卡中打开,如何使会话保持活动状态

时间:2014-05-20 08:46:12

标签: javascript php session

我想实现一件事。我在多个标签页面中打开了我的网站。当我在一个标签中工作时,其他标签不应该是超时的。它应该还活着。如何为其他标签保持活力。我在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>

请帮帮我。如何保持所有打开的tabs.thanks的会话

2 个答案:

答案 0 :(得分:0)

会话不是特定于标签的。它适用于整个浏览器实例。

当用户向服务器发出请求时会话被扩展。因此,根据您的应用程序,您可能必须生成对服务器的常规请求以扩展该会话。

为此,您可以使用setInterval javascript函数来触发对PHP后端的AJAX调用。

希望有帮助...

BTW:要小心,因为有些浏览器会停用没有用户关注的标签(例如Chrome) - 它不会修改会话...但是某些JS可能会被暂停。

答案 1 :(得分:0)

我能想到的一种方法是在服务器中自己处理会话。

这是一个可行的系统(欢迎改进建议):

1)将您自己的自定义会话存储在服务器数据库中,其中IP是唯一标识符。

2)每当用户与您的服务器交互时,获取其IP并更新存储在服务器中的相应会话。如果该会话已超时,请将用户重定向到超时页面。

可能有很多事情我没有通过,但在我看来,这应该是一个跨标签维护会话的可行方法