Automatic Login with Facebook PHP SDK doesn't works

天大地大妈咪最大 提交于 2019-12-11 18:09:44

问题


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.)



来源:https://stackoverflow.com/questions/10209723/automatic-login-with-facebook-php-sdk-doesnt-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!