检查用户是否已登录的最有效方法是什么


What is the most efficient way to check if user is logged in?

我对Facebook SDK和API非常熟悉,但我仍然发现自己在这方面遇到了很多麻烦。最重要的是速度!

每次我执行"api"调用时,都需要额外的一两秒钟才能返回响应。因此,我想知道检测用户是否通过Facebook登录的最有效、最快的方法是什么。我目前正在使用:

        // Get the User ID of the facebook user
        $facebook_user_id = $this->facebook->getUser();
        // If a facebook user exists, then try to get their information
        if($facebook_user_id){
            try{
                // Get the facebook users details
                $facebook_user_name = $this->facebook->api('/me','GET');
                $this->facebook_logged_in = $facebook_user_id;
                return $this->unique_id = $this->facebook_logged_in;
            }catch(FacebookApiException $e){
                error_log($e);
                return false;
            }
        }else{
            return false;   
        }

这在一定程度上有效,但速度非常慢。我真的想知道用户是否已经登录,他们的会话是否有效。还有别的办法吗?为什么这么慢

在大多数facebook的PHP示例中,它们倾向于使用$uid = $facebook->getUser();参见http://developers.facebook.com/docs/reference/php/facebook-getUser/

然后他们检查$uid是否为0,然后不登录。如果不是0,那么你有他们的facebook用户id。

看起来你在不需要的时候额外调用了/me命令。为了更快地调用/me,请指定fields=Comma,List,Of,Fields,You,Need,这样查询就可以更快地运行。但如上所述,为了获得登录状态,请在$uid中检查0。