使用Facebook PHP SDK自动登录不起作用 [英] Automatic Login with Facebook PHP SDK doesn't works

查看:57
本文介绍了使用Facebook PHP SDK自动登录不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用Facebook PHP SDK将facebook集成到我的网站中,我已经登录了我的facebook帐户,但是当我进入自己的网站时,会不会发生自动登录?

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();
}

在我进入facebook帐户后进入我的网页时, $ user 的值为'0'.

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

如果我通过网站的Facebook登录名登录.它可以正常工作,即 $ user 将获得一个ID.

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

推荐答案

但是当我进入自己的网站时,不会自动登录吗?

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

仅靠PHP SDK不能提供用户重新登录页面的自动登录,因为它无法读取在自己的域下设置的Facebook cookie.

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.

您必须将其与JS SDK结合使用,后者可以进行跨域请求,因此可以确定当前登录Facebook的用户以及他们是否访问过您的页面并曾经使用过您的应用.

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 是用于此目的的方法;如果它返回用户已经连接到您的应用程序,则您可能会触发从JavaScript重新加载页面,以便PHP SDK也将认识该用户.(如果您不重新加载页面,则PHP只会在用户下次单击页面中的链接并随后触发对服务器本身的下一个请求后才知道该用户.)

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

这篇关于使用Facebook PHP SDK自动登录不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆