Auto login with PHP facebook connect + JS

2019-09-09 11:44发布

问题:

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

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

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.

回答1:

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

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.

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

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.

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.



回答2:

Are you performing proper:

$facebook = new Facebook(array(
    'appId'  => $app_id,
    'secret' => $app_secret,
    'cookie' => true
));

before calling this method? Are you using same domain?