doucheyi1347 2012-08-23 07:56
浏览 42
已采纳

Facebook登录/注销/检查PHP类中由PHP-SDK提供支持的用户

I have PHP-Class to work with Facebook API:

class Social {
   private $name;
   private $context;
   private $tocken;

   public function check(){
     $this->tocken = $this->context->getAccessToken(); // get current tocken
     if (isset($_SESSION['fb_tocken']) AND !empty($_SESSION['fb_tocken'])){
        // session tocken not null
        if ($_SESSION['fb_tocken'] == $this->tocken){
           // session tocken = current tocken
           // update last active
           return true;
        } elseif ($this->tocken == APPLICATION_TOCKEN) {
          // current tocken == default tocken
          $this->logout();
        } else {
          // current tocken <> default tocken
          $this->logout();
        }
     } else {
        // session tocken is null
        if ($this->tocken == APPLICATION_TOCKEN){
          // current tocken is default
          return false;
        } else {
          // current tocken is not default
          if ($this->getUserInfo()){
             // user registered - login action
             if ($this->login())
                return true;
             else
                return false;
          } else {
             // register new user
             if ($this->register()){
                if ($this->login()){
                   return true;
                } else
                   return false;
             } else
                return false;
          }
        }
  }
   }
   // ...
};

If user used alogritm:

1 - Login in facebook 2 - Login in application 3 - Logout from facebook 4 - Login in facebook 5 - Open application

Application Problem:

after fifth step my app can't authorize a user. But if user press 'refresh' he become authorized.

  • 写回答

1条回答 默认 最新

  • douyi02577 2012-08-23 08:40
    关注

    That is the correct behviour - but impossible to confirm without the rest of your code (i.e. the bit that grabs the token).

    What happens is that when a user logs out, the current token (that you have stored in your session) becomes invalid. The user logs back into Facebook, you need to get the new token, and that's not going to happen automtically - but when you do a refresh, I presume your code getAccessToken() (not shown above) will grab the new token.

    The simplest fix is to add a listener in Javascript for logging in and out. You can find the details here and you need the auth.login handler. When you have that triggered, simply refresh the page.

    There are other options using Javascript tot pump the new token to a PHP listener that can update your session behind the scenes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable