简体   繁体   中英

$facebook->getUser() returns a userid after logout

How do I check if the user is really loggedin? $facebook->getUser() still returns an ID after logout. Do I need to do something like $facebook->api('/me') just to check if the user is "really" logged in?

Well, FB PHP SDK in my opinion is quite tricky because it relies on a cookie sent from Facebook when you are logging into the Facebook. This cookie is not deleted whilst logging out. Because of that in below code the variable $uid could store a proper user facebook id:

$uid = $facebook->getUser();

So, as far as I know, call $facebook->api('/me'); will tell the truth whether the user is logged in or not.

try {
   $facebook->api('/me');
   /* user is really logged into FB */
} catch (Exception $e) {
   /* user is not currently logged into the FB */
}

I use above code in my production application and it works well.

getLoginStatusUrl should do the trick. I think that if you ask for offline_perms than you have access to user_id and so on. I don't know if you did, but most likely that should be the problem

Have you tried the method detailed in the Facebook PHP SDK Documentation ?

$params = array(
  'ok_session' => 'https://www.myapp.com/',
  'no_user' => 'https://www.myapp.com/no_user',
  'no_session' => 'https://www.myapp.com/no_session',
);

$next_url = $facebook->getLoginStatusUrl($params);

Returns a URL based on the user's login status on Facebook. You can get a different URL depending on whether the user is logged in, not connected, or logged out of Facebook.

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