changed
Title
Get notified about the statusaof a Facebook user in your web app using periodic Ajax calls.
Get notified about the statusaof a Facebook user in your web app using periodic Ajax calls.
hello,
This article is helpful only if you have the following situation:
A web app which uses facebook login to login its users and if a user is
logged out from his/her facebook account through another browser tab, then your
app dont know that he is logged out out or not from his facebook account and
he/she will remain as logged in your web application.Its ok if you have no
problem with this situation.But if you want to forcefully logout the user from
your application if he is logged out from his facebook account means you can try
this example.This example uses periodic ajax calls to facebook's graph api
to check whether the user is logged in or not.
~~~
[php]
<script type="text/javascript">
var loginStatus=null;
window.fbAsyncInit = function() {
FB.init({
appId : "<?php echo
Yii::app()->params['facebook_appId'] ?>",
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
/*This will work only at onload*/
FB.getLoginStatus(function(response) {
if (response.status === 'connected')
{
loginStatus='online';
}
else{
loginStatus='offline';
}
});
/*The function used for ajax calls */
function checkStatus()
{
FB.api('/me', function(response) {
var isGuest="<?php if (Yii::app()->user->isGuest)
echo 'true'; else echo 'false'; ?>";
if(response.name==undefined&&loginStatus=='online'&&isGuest=='false')
{
window.location="<?php echo
Yii::app()->createAbsoluteUrl('site/logout'); ?>";
}
});
}
setInterval(function(){
checkStatus();
},5000);
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id))
{return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
~~~
I have posted a working code above.anyway let me know if you have any problem
with this.
-Sirin