1

The question might sound a bit dull at first and I'm baffled myself.. I'm developing a PHP application that relies heavily on the Facebook API, thus is using the PHP SDK to connect and getting the currently logged in user via

$fb->getUser();

I have also included the Javascript SDK but its currently only fetching the logged in user and prints their data object to the console.

Today I began to implement a local dev-version of the app, set up a second dev-app on Facebook, connected them and voila.. it doesn't work. getUser() always returns 0 for me locally. What I just found out though is that it works in Firefox. I var_dump the getUser() value, and recieve a full user id (mine) in Firefox, but 0 in Chrome (and yes, I'm logged in to Facebook on both browsers).

I cleared the cache, cookies, destroyed the session.. I just cant imagine what difference the browser makes for a PHP request to Facebook? Has anybody ever experienced anything like this? Obviously the app_key and secret must be correct because it works on Firefox.


Edit: just for reference, the live version of the application (hosted) works well in Chrome, it's only this very local app that I can't get to work.


Edit#2: This is the Javascript that I run on the app

  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : {$_connect.app_id}, // App ID
        channelUrl : '{$_connect.channel_file}', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
    {if !$_user.is_authorized}
    // listen for and handle auth.statusChange events
    FB.Event.subscribe('auth.statusChange', function(response) {
      if (response.authResponse) {
        // user has auth'd your app and is logged into Facebook
        FB.api('/me', function(me){
          if (me.name) {
            console.log(me);
            //window.location = 'index.php';
            // redirection is currently causing loop
          }
        })
      } else {}
    });
    {/if}
    };
    // 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/de_DE/all.js";
       ref.parentNode.insertBefore(js, ref);
     }(document));
  </script>
6
  • May be you have not allow facebook application in chrome. Because i have also used facebook SDK recently and it is working fine in all browsers. Sep 14, 2012 at 12:02
  • @YogiYogesh the live version of the app does work in Chrome, so it can't be that easy, thank you though! Sep 14, 2012 at 12:03
  • i have same problem, let me know how did you solve it. May 27, 2013 at 18:39
  • @bluepicaso I didn't really. It worked on the live version with exactly the same setup and the same configs on both sides so I decided to not dig any deeper in this to save time and get the project done. I had to set up a more simple facebook based login system in a recent project and got it running just like that, so I guess I can't be of any extensive help to you on this, sorry :\ May 28, 2013 at 11:04
  • well i worked it out with javascript SDK instaead of PHP May 28, 2013 at 21:02

2 Answers 2

1

I just cant imagine what difference the browser makes for a PHP request to Facebook?

The only thing, of course, can be the parameters.

And in this scenario, it’s most likely the cookies – the cookies, that Facebook sets on successful login and that the browser has to accept and send back to the server, for the PHP SDK to be able to recognize the logged in user.

So, check if your Chrome accepts these cookies when you are testing your app locally.

4
  • Thanks for the heads up. I checked back and it seems that there are no cookies from facebook in the context of my local app. Either Chrome doesn't allow them or they are never created because of some other error. Sep 14, 2012 at 12:17
  • Scrap that, facebook cookies are created. But it still only gives me 0 by getUser(). Sep 14, 2012 at 12:23
  • Then check for cookies under your own (local) domain/hostname as well.
    – CBroe
    Sep 14, 2012 at 13:01
  • Update, sorry for the late accept. I still did not solve this in Chrome but this seems to be the only valid explanation for my problem. I got rid of the complete PHP SDK and am only handling Facebook via Javascript in the client, thus the problem itself dissapeared and the page is a whole lot faster now. Sep 25, 2012 at 9:40
1

I think, you need to authenticate via JS SDK in both browsers also, I believe there's some OAuth logic behind JavaScript requests.

However, Chrome nowadays tries to block or simply not execute cross domain js requests. Try looking into net console if anything like that happens to you. If it does

  • Try checking domain + protocol /http(s)/ of your local environment. I believe you should be using https while calling js api
  • Consider using channel file, which should solve / prevent these crossbrowser calls issues as described in JavaScript SDK Documentation

If nothing from above helps you, I guess you could develop the app, test it on FFox and then once it's deployed test it with real setting.

Good luck.

3
  • Hi Nitram, thanks for the answer. I indeed forgot to change the url to the channel file to the local path, that however didn't solve the problem either. As said above, I do include the JS-SDK too and am able to recieve the user data from it (so it basically works). But the PHP SDK is still returning 0. I have compared the app_key and the like 10 times, it's all correct. Thanks never the less :) Sep 14, 2012 at 15:03
  • Do you just include the JS SDK or are you calling FB.login or FB.getLoginStatus if you've been logged before, after the api initialization? That turned out to be helpful for me (even though I own the app and I maybe shouldn't need to do that), to actually make OAuth authorize you with the app. I had some troubles with this myself, but the login from JavaScript basically solved it (i guess) Sep 14, 2012 at 16:16
  • I included the Javascript I use on the page in the question. I don't call login or getLoginStatus but I observe statusChange. This works and I recieve the full user object on the console. Sep 16, 2012 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.