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?
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.
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.
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.
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.
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.
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.
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:
$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] );
$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] );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With