Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle the user logout in browser multiple tab?

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 other tab. Is this possible?

like image 393
Nadeshwaran Avatar asked Feb 11 '14 03:02

Nadeshwaran


People also ask

How do you handle the user logout in browser multiple tab react?

There is a neat little way to log out all the tabs if the user clicks logout from one tab. Since we will be maintaining a token in localStorage. We can add an event listener for our storage and check if the token becomes null then, we'll dispatch a logout action to logout the user from all the open tabs.

How user will logout from all open tabs automatically when user logs out in one of them?

setItem('logout-event', 'logout' + Math. random()); Every other tab will get it with a listener. You can use both approaches - localStorage and auto-refresh, for example every 60 seconds, just to be sure it works when tabs are opened at different computers.

How do you prevent user to open same URL in multiple tab in same browser in react?

There is nothing you can do to prevent a user from opening multiple tabs and opening the same url in every tab. You could store a cookie in localStorage to see if they opened another tab or not, but that won't work for different browsers or incognito mode.

How can we prevent user from opening the same URL in multiple tabs in the same browser in Angularjs?

You cannot (and should not) do that. User can always just open use another browser or another computer to open another view onto the web site. So, basically you cannot ever prevent this. Your web-site should be able to handle multiple tabs viewing the same state.


2 Answers

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.

like image 155
SpoonNZ Avatar answered Oct 06 '22 01:10

SpoonNZ


Here is my below code to solve the issue. I set the cookie While login and deleted at logout. viceversa at Logout and Login also.

Script:

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;
}

function setCookie(cname,cvalue,exdays)
{
var d = new Date();
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
} 

window.setInterval(function() {
if(readCookie('loggedout')==1) {

    window.location.reload();

   setCookie('loggedout',2,3);
    //Or whatever else you want!
}
 else if(readCookie('loggedin')==1) {

    window.location.reload();

   setCookie('loggedin',2,3);
    //Or whatever else you want!
}
},2000)

Controller:

Login:

$this->load->helper('cookie');
    $cookie = array(
'name'   => 'loggedin',
'value'  => '1',
'expire' => '86500'
);
set_cookie($cookie);
$domain= preg_replace("/^[\w]{2,6}:\/\/([\w\d\.\-]+).*$/","$1", $this->config->slash_item('base_url')); 
$path = explode($domain,base_url());
delete_cookie('loggedout');
    delete_cookie('loggedout',$domain, $path[1] ); 

Logout:

$cookie = array(
'name'   => 'loggedout',
'value'  => '1',
'expire' => '86500'
);
set_cookie($cookie);
$domain= preg_replace("/^[\w]{2,6}:\/\/([\w\d\.\-]+).*$/","$1", $this->config- >slash_item('base_url'));
$path = explode($domain,base_url());
delete_cookie('loggedin');

delete_cookie('loggedin','localhost', '/<!-- Your path -->/'); 

delete_cookie('loggedin',$domain, $path[1] ); 
like image 27
Nadeshwaran Avatar answered Oct 06 '22 00:10

Nadeshwaran