简体   繁体   中英

Check if a user is logged in to Facebook

I'm trying to check whether a user is logged in to Facebook (just logged in or not, with no relation to my application) using javascript.
I tried using the following code after FB.init:
FB.getLoginStatus(function(response) {
alert(response.status);
});

edge.create is working just fine though.

There are some hacks that can help, but not in your situation. https://grepular.com/Abusing_HTTP_Status_Codes_to_Expose_Private_Information

Here is simple example:

<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> 
if (FB.getSession() != null) {
    FB.api('/me', function(response){
        alert ("Welcome " + response.name);
    })
}

With one caveat, though: the user needs to authorize your app in order to detect whether is logged in or not. I believe there is no way to detect it without it with this api: You can add a like button and such, but not interact with the fb status.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM