检查用户是否登录新的Facebook API [英] Check if user is logged in New Facebook API

查看:164
本文介绍了检查用户是否登录新的Facebook API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我是Facebook的新闻,最近有人告诉我我在使用旧的api。



所以现在,我似乎不能以了解如何检查用户是否使用新的api登录到我的网站。



我真正拥有的是

 < div id =fb-root>< / div> 
< script src =http://connect.facebook.net/en_US/all.js>< / script>
< script>
FB.init({appId:'(appid)',status:true,cookie:true,xfbml:true});
FB.Event.subscribe('auth.sessionChange',function(response){
if(response.session){
//用户已登录,并且新的cookie已被保存
} else {
//用户已经注销,并且已清除cookie
}
});
< / script>

和登录按钮。



如果任何人都可以告诉我如何检查某人是否登录,(或者是在那里吗?),因为我想改变位于一个名为



< div id =fbuser>



欢迎用户。 b
$ b

或者即使你可以指出一些有用的教程,那将是非常棒的!



谢谢

解决方案

这里有一个例子,当用户登录/日志时,会自动调用 fbLoginStatus()函数出来,所以你可以决定在哪里显示哪个div。

 < body> 
< div id =fb-root>< / div>
< script>
函数fbLoginStatus(响应){
if(response.status ==='connected'){
//用户登录,显示配置文件div
} else {
//用户未登录,显示guest div
}
}
window.fbAsyncInit = function(){
FB.init({appId:'您的应用程序ID ',status:true,cookie:true,xfbml:true});
FB.getLoginStatus(fbLoginStatus);
FB.Event.subscribe('auth.statusChange',fbLoginStatus);
};
(function(){
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
' //connect.facebook.net/en_US/all.js';
document.getElementById('fb-root')。appendChild(e);
}());
< / script>

现在,如果您需要检查用户是否正在登录(例如按钮点击):

  if(FB.getSession()!= null){
// logged用户ID:FB.getSession()。uid
} else {
//用户未登录
}

对不起,不知道任何好的教程。


well I'm new to the facebook api stuff, and just recently someone told me I was using the old api.

So now, I can't seem to figure out how to check if the user is logged into my site with the new api.

all I really have is

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({appId: '(appid)', status: true, cookie: true, xfbml: true});
  FB.Event.subscribe('auth.sessionChange', function(response) {
    if (response.session) {
      // A user has logged in, and a new cookie has been saved
    } else {
      // The user has logged out, and the cookie has been cleared
    }
  });
</script>

and a login button.

If anyone could show me how to check if someones logged in, (or is that it right there?) because I'd like it to change the login button located in a div called

<div id="fbuser">

to welcome the user.

Or even if you can point me towards some useful tutorials, that would be great!

Thanks

解决方案

Here is an example where fbLoginStatus() function will be automatically called when user logs in/logs out, so you can decide which div to display there.

<body>
<div id="fb-root"></div>
<script>
  function fbLoginStatus(response) {
     if( response.status === 'connected' ) {
        //user is logged in, display profile div
     } else {
        //user is not logged in, display guest div
     }
  }
  window.fbAsyncInit = function() {
    FB.init({appId: 'your app id', status: true, cookie: true, xfbml: true});
    FB.getLoginStatus(fbLoginStatus);
    FB.Event.subscribe('auth.statusChange', fbLoginStatus);
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

Now if you need to check if user is logged in right now (for example on button click):

if(FB.getSession() != null) {
   //logged user id: FB.getSession().uid
} else {
  //user is not logged in
}

Sorry, don't know any good tutorials.

这篇关于检查用户是否登录新的Facebook API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆