简体   繁体   English

如何检查用户是否使用新的facebook php api登录

[英]How to check if user is logged in with new facebook php api

I am migrating my old FB app to the new graph API using the PHP API 我正在使用PHP API将旧的FB应用程序迁移到新的图形API

I have two pages: public ones (which require no user login) and private ones (which do) 我有两个页面:公共的(不需要用户登录)和私人的(有)

So the code of every single php page in my application works as follows: 所以我的应用程序中每个php页面的代码如下:

if (this_page_requires_user_login){
      $facebook = new Facebook(...) ;
      $session = $facebook->getSession ;
      if (!$session){
              $url =$facebook.getLoginUrl(array(next => current_page );
              echo "<fb:redirect url=$url/>" ;

}
// the rest of the code continues here

Now as you can see, this way of working forwards every single page to the login url and while it works, it is also slow and appends the &session={bla} string to very single url. 现在您可以看到,这种工作方式将每个页面转发到登录URL,当它工作时,它也很慢并且将&session = {bla}字符串附加到非常单个URL。

I need a way to check where a user is already logged in before I redirect him to loginpage. 在将用户重定向到登录页面之前,我需要一种方法来检查用户已登录的位置。 But i can not find such method in the php api. 但我在php api中找不到这样的方法。 What's the best way to do this? 最好的方法是什么?

EDIT 编辑

This seemed to do the trick 这似乎可以解决问题

if ($session) {
  try {
    $me = $facebook->api('/me');
    if ($me) {
       // here comes your code for user who is logged in
    }
  } catch (FacebookApiException $e) {
    login()
  }
}else{
 login()
}

function login(){

  $url =$facebook.getLoginUrl(array(next => current_page );


      echo "<fb:redirect url=$url/>" ;
}

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 , even if you get a session back, you can't assume it's still valid. 根据Facebook的例子中的评论,即使你回来了会议,你也不能认为它仍然有效。 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
  }
}

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

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