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
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] );