Friday, March 11, 2011

Gmail style auto logout

Hi,

Assume you are logged into gmail and have opened various instance of gmail, by creating new tab or new window of the same browser. Now when you logout from any one of the instance you get logged out from all the instances. This code does exactly the same. This is how it works:


The idea here is to run a script continously in the backgroud, which always keeps checking the current session status. Whenever it finds that the session doesn't exist, it just redirects the current page to login.php. Javascript is the key, and the function which is making this happen is setTimeout(). Since I have used ajax it will not effect the actual page's performance. This is the code that got the ball rolling:


jQuery(document).ready(function(){
thread();
})

function thread()
{
$.ajax({
 type: "POST",
 url: "thread.php",
 success: function(msg){
if(msg=="-1"){document.location.href="login.php";}
setTimeout("thread()",500)
}
});
}

Click here to download the entire source code. In login page just enter any username and password to login. I am sure you can put in the correct validation for that :)

Cheers!!!


No comments:

Post a Comment