How to handle the user logout in browser multiple tab?

后端 未结 2 1683
闹比i
闹比i 2021-01-02 18:24

I am using codeigniter session to store user login. When user opens in multiple tab, on logout in one tab , I want the page to be auto refreshed while user visiting the othe

2条回答
  •  借酒劲吻你
    2021-01-02 18:53

    Just tested it out, and the easiest way I can see (which seems to work in Chrome at least, but may need further testing) is setting a cookie.

    On logout do something like setcookie('loggedout',1). You'll also need to do the opposite on login - unset($_COOKIE['loggedout'])

    Then you just need some simple Javascript...

    function readCookie(name) {
        var nameEQ = escape(name) + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) === ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
        }
        return null;
    }
    window.setInterval(function() {
        if(readCookie('loggedout')==1) {
            window.location.assign('loggedout.html')
            //Or whatever else you want!
        }
    },1000)
    

    That'll check each second to see if the cookie is set. Magic.

提交回复
热议问题