简体   繁体   English

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

[英]how to keep the session active even i opened in multiple tabs in same browser

i would like to implement one thing.i opened my site in multiple tabs. 我想实施一件事情。我在多个标签中打开了我的网站。 while i work in one tab others tabs should not be timedout. 当我在一个选项卡中工作时,其他选项卡不应超时。 it should be alive. 它应该还活着。 how to keep alive for other tabs. 如何为其他标签保持生命。 i used below js to find idle time logout. 我用下面的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>

Please help me. 请帮我。 how to keep the sessions for all opened tabs.thanks in advance 如何保持所有打开的选项卡的会话。

The session is not tab specific. 会话不是特定于选项卡的。 It is for the whole browser instance. 它适用于整个浏览器实例。

The session is extended when a user makes a request to the server. 当用户向服务器发出请求时,会话将扩展。 So depending on your application, you may have to generate regular requests to the server to extend that session. 因此,根据您的应用程序,您可能必须向服务器生成常规请求以扩展该会话。

For that, you can use the setInterval javascript function to trigger an AJAX call to your PHP backend. 为此,您可以使用setInterval javascript函数来触发对PHP后端的AJAX调用。

Hope that helps... 希望有帮助...

BTW: be careful because some browsers do deactivate the tabs which don't have the user focus (Chrome for example) - it will not modify session... but some JS could be suspended. 顺便说一句:请小心,因为某些浏览器会停用没有用户焦点的选项卡(例如Chrome)-它不会修改会话...但是某些JS可能会被暂停。

One of the way I can think of is handle the session yourself in the server. 我能想到的一种方法是自己在服务器中处理会话。

Here's a system that might work (welcome suggestions of improvement): 这是一个可能有效的系统(欢迎提出改进建议):

1) store your own custom session in server DB, with IP being the unique identifier. 1)将自己的自定义会话存储在服务器数据库中,其中IP是唯一标识符。

2) whenever user interacts with your server, grab its IP and update the corresponding session stored in server. 2)每当用户与您的服务器交互时,获取其IP并更新存储在服务器中的相应会话。 If that session has already timeout, redirect user to timeout page. 如果该会话已经超时,则将用户重定向到超时页面。

there might be many things that I haven't though through, but in my mind this should be a viable way to maintain session across tabs 可能有很多事情我还没有做完,但是在我看来,这应该是维护跨标签会话的一种可行方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React 中与其他打开的浏览器选项卡共享 Session 存储数据 - How to share Session storage data with other opened browser tabs in React 如何在浏览器中获取所有打开的标签会话存储? - How to get all opened tabs session Storage in browser? 如何避免为同一链接创建新标签(已在浏览器中打开) - How to avoid creating new tabs for the same link(already opened in the browser) 即使关闭并重新打开浏览器,如何使用Java HTTPSession在我的站点上保持会话打开? (不是PHP) - How can I keep the session open on my site using Java HTTPSession even by closing and reopening the browser? (NOT PHP) 如何让假浏览器标签集中在多个标签上? - How to keep fake browser tab focus on multiple tabs? 如何向在socket.io中的不同选项卡或浏览器中打开的同一用户发出事件? - How to emit event to same user which is opened in different tabs or browser in socket.io? 如何使用 java 脚本在同一浏览器的另外五个选项卡中打开 URL 已经打开? - how to open a URL is already opened in another five tabs of same browser using java script? 跨多个浏览器选项卡的JavaScript会话管理 - JavaScript session management across multiple browser tabs 多个标签页属于同一会话,所有标签页都消失后清除会话 - Multiple tabs same session, clear the session when all tabs are gone 如何在不使用JQuery的情况下通过对服务器的基本ajax调用基于浏览器上的用户活动来保持服务器会话处于活动状态 - How to keep server session active based on user activity on the browser with basic ajax call to server without JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM