用PHP facebook connect + JS自动登录 [英] Auto login with PHP facebook connect + JS

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

问题描述

由于Facebook在过去一年中所做的所有改变,很多事情都发生了变化。
在网络上有一些解决方案,但它们不再工作。
(喜欢这个:使用PHP或JavaScript Facebook SDK,我如何保持用户登录到我的网站?

since all the changes facebook made in the past year lot of things are changed. there are some solutions over the web but they do not work any more. (LIKE THIS ONE: Using the PHP or JavaScript Facebook SDK, how can I keep the user logged in to my website?)

我已经在我的网站上实现了与Facebook facebook SDK的Facebook连接。
问题是已经批准我的应用程序的用户,并且也登录到他们的Facebook。
在这种情况下,我希望他们自动登录,无需再次按连接按钮。
我想有一种方法可以使用php + js,但是我找不到如何和相信我尝试了几件事情。问题是php用户检查

I have implemented facebook connect with PHP facebook SDK on my website. the problem is with users that already approved my App and that also logged in to their facebook. In this case I want them the auto login with no need to press the connect button again. I guess that there is a way to do it using php+js but I couldn't find how and believe me I tried several things. the problem is that the php user check

$user  = $facebook->getUser(); 

不会识别Facebook会话,我认为也许我可以用JS触发它。
所以我累了添加这个代码在页面在JS和ALRETS正在说用户是连接,但没有会话传递给PHP,所以PHP SDK不识别用户连接:

doesn't recognized facebook session and I think that maybe I can trigger it with JS. so I tired adding this code for the page in JS and the ALRETS are working saying user is connected but NO session pass to PHP, so PHP SDK doesn't recognize user is connected :

<div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
              appId  : '<?=FACEBOOK_APP_ID?>',
              status : true, // 
              cookie : true, // 
              xfbml  : true, //
              oauth  : true
        });

      // Additional initialization code here
 //--------> this isn't need to work its only a check
FB.getLoginStatus( function(response) {
    //console.log(response);
    if (response.status === 'connected') {
        var accessToken = response.authResponse.accessToken;
        alert(accessToken);
    } else if (response.status === 'not_authorized') {
        //login function
    } else {
        //login function
    }
}, true);

FB.Event.subscribe('auth.authResponseChange', function(response) {
    //console.log('The status of the session changed to: '+response.status);
    alert(response.status);
});

 //--------> END OF CHECK!!
      };
      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/<?=$fb_api_lang?>/all.js#xfbml=1";
         ref.parentNode.insertBefore(js, ref);
       }(document));
    </script>

如果有人找到一个解决这个问题的解决方案,并且可以分享它将不胜感激。

if anyone found a solution for this problem and could share it will be much appreciated.

推荐答案


问题是php用户检查 $ user = $ facebook-> getUser(); 不会识别Facebook会话

the problem is that the php user check $user = $facebook->getUser(); doesn't recognized facebook session

当然,它不能 - 因为服务器端只能访问为您的域设置的cookie。但Facebook无法为您的域设置Cookie,而无需用户访问该域并与Facebook进行交互。

Well of course it can’t – because server-side it can only access the cookies set for your domain. But Facebook can not set a cookie for your domain without the user being on that domain and interacting with Facebook.


我认为也许我可以用JS触发它。

and I think that maybe I can trigger it with JS.

只需嵌入JavaScript SDK,初始化它并设置 cookie:true 参数。这将使JS SDK在您的域下设置一个cookie,哪个PHP可以在下一个请求上阅读

Just embed the JavaScript SDK, initialize it and set the cookie: true parameter. That will make the JS SDK set a cookie under your domain, which PHP can read on the next request.

如果要识别返回用户立即,则您必须检查客户端,并在满足适当条件时重新加载页面:JS SDK识别返回用户( FB.getLoginStatus

If you want to recognize a returning user "immediately", then you would have to check client-side for this, and reload your page once when the appropriate conditions are met: JS SDK recognized returning user (FB.getLoginStatus), but PHP did not recognize him yet (have your PHP code set a variable/flag, that your JS can evaluate) – then reload page via JS, so that PHP can read the cookie that was just set by the client-side JS.

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

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