Automatic Login with Facebook PHP SDK doesn't

2019-09-06 22:02发布

问题:

I integrated facebook with my site using Facebook PHP SDK, I have logged into my facebook account, but when I enter into my website, automatic login doesn't happens ?

    $facebook = new Facebook(array(
      'appId'  => XXXXXX,
      'secret' => XXXXXXXXXXXXXX,
    ));



$user = $facebook->getUser();

// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl();
}

Here $user value is '0', while entering to my webpage after I enter into facebook account.

If I login through my website's facebook login. It works i.e $user will get an id.

回答1:

but when I enter into my website, automatic login doesn't happens ?

The PHP SDK alone can not provide for automatic login of users re-visiting your page, because it can not read Facebook’s cookies set under their own domain.

You have to combine it with the JS SDK, which can do cross-domain requests and therefor is able to determine the user currently logged in to Facebook and whether they have visited your page and used your app before.

FB.getLoginStatus is the method to use for that; if it returns that the user is already connected to your app you might to trigger a reload of your page from JavaScript, so that the PHP SDK will know the user as well. (If you don’t reload your page, PHP will only know the user after the next time they click on a link within your page and thereby trigger the next request to your server themselves.)