简体   繁体   English

如何检查用户是否登录到Facebook

[英]How to check if a user is logged in to facebook

I am totally new to facebook api and I wanted to ask , whats the easiest and fastest way to know if a person is logged in to facebook. 我对Facebook api完全陌生,我想问一下,知道一个人是否已登录Facebook的最简单,最快的方法是什么。

One solution that I tought of is to send a javascript or php request and see if i get some status code that tells me whether the person is logged in or not (say if it returns 404, then that means he isnt logged in). 我要解决的一个解决方案是发送javascript或php请求,看看我是否得到一些状态代码来告诉我该人是否已登录(例如,如果该用户返回404,则表示他未登录)。

update: 更新:

I found this easy "hack": 我发现这个简单的“ hack”:

    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()">

But it doesnt work... however, thats the type of code that i am looking for..code that would send a request and if fails, it will throw an error 但这不起作用...但是,那是我正在寻找的代码类型..代码会发送请求,如果失败,它将抛出错误

**update**

I dont want to trigger any suspicion from the user that I am checking if he is logged in. So if there is a use of app_id, then I would think that I need the users permission to use my app..that is a burden cause I will have to take another authentication step. 我不想引起用户怀疑我正在检查他是否已登录。因此,如果使用了app_id,那么我认为我需要用户许可才能使用我的应用程序。这是一个负担原因我将不得不采取另一个身份验证步骤。

I cant have the user know that he is being authenticated in any way regarding facebook 我无法让用户知道他正在以任何方式通过有关Facebook的身份验证

If I am reading your code correctly, only if no session is returned do you do the redirect? 如果我正确地阅读了您的代码,则仅当没有会话返回时,您才进行重定向吗? According to comments in Facebook's example: http://github.com/facebook/php-sdk/blob/master/examples/example.php (great place to this info huh?), even if you get a session back, you can't assume it's still valid. 根据Facebook示例中的评论: http : //github.com/facebook/php-sdk/blob/master/examples/example.php (此信息的绝佳去处吗?),即使您重新获得会​​话,也可以认为它仍然有效。 Only trying an API call that requires a logged in user will you know for sure. 您肯定会知道,仅尝试进行需要已登录用户的API调用。 This is the best way I've seen to reliably determine login/logout status. 这是我见过的可靠确定登录/注销状态的最佳方法。

if ($session) {
  try {
    $me = $facebook->api('/me');
    if ($me) {
      //User is logged in
    }
  } catch (FacebookApiException $e) {
    //User is not logged in
  }
}

try This, Hope this will help 试试这个,希望这会有所帮助

include 'facebook.php';
$facebook = new Facebook(array(
 'appId'  => $app_id, // your application id
 'secret' => $app_secret, // your application secret
'cookie' => false,
));

$user = $facebook->getUser();


$loginUrl   = $facebook->getLoginUrl(
        array(
            'scope' =>  '// put your permission'
        )
);

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$permissions = $facebook->api('/me/permissions');   
   } catch (FacebookApiException $e) {
    $user = null;
  }
 }  

 if (!$user) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
  } 

If you have a facebook "tab" then limited user information is provided to you as part of the signed_request. 如果您有一个Facebook“标签”,那么在signed_request中会向您提供有限的用户信息。 And for a facebook tab, you need to create an app. 对于facebook选项卡,您需要创建一个应用程序。

Otherwise, without an App ID and alerting the user YOU CANNOT DO IT. 否则,没有应用程序ID并警告用户,您将无法执行此操作。

There have been hacks in the past, some of the cleverer ones have been to put up comment boxes and measure the hieght of the containing DIV (they used to be larger if logged in) or to include the facebooks FBML tabs to show a log in button (log in was wider) but theses were all stamped out as they were discovered. 过去曾发生过一些骇客活动,其中一些更聪明的举动是放置评论框并衡量所含DIV的高低(如果已登录,它们通常会更大)或包括Facebook FBML选项卡以显示登录信息按钮(登录范围更广),但是这些被发现时便全部删除。 The same probably goes for the link you tried in your question. 您在问题中尝试的链接可能也是如此。

Facebook, for all the debate on rpivacy, won't knowlingly leak information - until you get an app id, ask the user then you can get nearly everything... 在所有关于复制性的辩论中,Facebook不会故意泄漏信息-直到您获得应用程序ID之前,询问用户您便可以获取几乎所有内容...

Check this topic: https://stackoverflow.com/a/10698594/1789650 检查此主题: https : //stackoverflow.com/a/10698594/1789650

This should answer your question. 这应该可以回答您的问题。 You have to load the javascript SDK to get it to work correctly. 您必须加载javascript SDK才能使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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