简体   繁体   中英

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

I am pretty familiar with the Facebook SDK and API but I still find myself having a lot of trouble with it. Most of all, speed!

Each time I execute an 'api' call, it takes an additional second or two to return a response. I would therefore like to know what the most efficient and fastest way to detect whether a user is logged in via Facebook is. I am currently using:

        // 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;   
        }

This works to a certain extent but it is awfully slow. I literally want to know whether the user is logged in and their session is valid. Is there another way to go about doing this? Why is it so slow?

In most all of facebook's PHP examples, they tend to use $uid = $facebook->getUser(); see http://developers.facebook.com/docs/reference/php/facebook-getUser/

And then they check if the $uid is 0, then not logged in. if other than 0, then you have their facebook user id.

Looks like you're calling the /me command additionally when you really don't need to. Also to make the /me call quicker, specify the fields=Comma,List,Of,Fields,You,Need so the query can operate quicker. But as noted above, for just getting login status, check for 0 in $uid.

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