检查用户是否已登录Facebook

时间:2011-04-13 19:25:22

标签: javascript facebook

我正在尝试使用javascript检查用户是否已登录到Facebook(刚刚登录或未登录,与我的应用程序无关)。 我在FB.init之后尝试使用以下代码:
FB.getLoginStatus(function(response) {
alert(response.status);
});

edge.create工作得很好。

3 个答案:

答案 0 :(得分:6)

有一些黑客可以提供帮助,但不适合你的情况。 https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information

这是一个简单的例子:

<html> 
<head> 

</head>

<body>
// At the end of page will be good 
<script>
function logged() {
    alert('logged');
}
function notlogged() {
    alert('not logged');
}
</script>
<script src="http://www.facebook.com/ajax/composer/attachment/question/question.php" onload="logged()" onerror="notlogged()">
</body> 
</html> 

答案 1 :(得分:0)

if (FB.getSession() != null) {
    FB.api('/me', function(response){
        alert ("Welcome " + response.name);
    })
}

但有一点需要注意:用户需要授权您的应用以检测​​是否已登录。我相信没有这种api就没有办法检测它:你可以添加一个类似按钮等但不与fb状态交互。

http://developers.facebook.com/docs/guides/web/

答案 2 :(得分:0)

FB.getLoginStatus(function(response) {
if (response.authResponse) {
                // alert('Welcome!  Fetching your information.... ');
                 FB.api('/me', function(response) {
                   alert('Good to see you, ' + response.name + '.');
                   });
               }
});