3

Recently I've uploaded a piece of code to phpfog and I've come across with a problem which didn't happen locally:

When the page loads, it tries to get $idFacebook that is supposed to be set:

$idFacebook = $facebook->getUser();
if ($idFacebook) { ... }

I'm logged to Facebook, however, the condition fails and the button to login appears:

<div class="fb-login-button" data-scope="user_likes,user_photos"></div>

The other problem is that when I press the button, it seems that the login window appears but it closes automatically very quickly.

My code is basically the same that Heroku provides when you create a Facebook app. The repository is this one: https://github.com/heroku/facebook-template-php. I can't find how to solve it and I've seen that lots of people have experienced several problems with facebook login.

I'm looking forward you can help me. Thanks in advance.

4 Answers 4

4

My actual error was that $facebook->getUser() returned 0 because the app wasn't authenticated and the app wasn't authenticated because I moved my code from a host to another in a different domain and I forgot changing the application configuration in http://developers.facebook.com.

Now, the app gets the login automatically. Thanks everybody!

1
  • I had the same problem too. All problem was in Sandbox radiobutton in app settings.
    – Rustam
    May 3, 2013 at 20:51
1

The login window disappearing quickly is Facebook checking whether you need to grant permissions to the app or not. It is closing almost instantly because you are already logged in to and authorised with Facebook.

I'm not familiar with the Heroku / Facebook integration but I would assume the $idFacebook problem is to do with invalid configuration.

1
  • I agree with you but locally, it worked fine. I don't know what's wrong and I really don't know how the server running php gets the cookie of the client who has executed the JS code to log in FB. Thanks!!
    – Jonás
    Jul 25, 2012 at 19:40
1

The login window is dissappearing quickly because you may be logged in to facebook in same browser.

I'm not familiar with Heroku but I've overcome this issue in Zend Framework - php. I've used javascript-sdk for login and logout. You may refer this link.

http://developers.facebook.com/blog/post/2011/07/21/updated-javascript-sdk-and-oauth-2-0-roadmap/

So when you implement this example in your code It will ask you to logout when you logged-in in to FB in same browser. so when you click logout , It will again ask you to log-in and Thats it.

Hope it helps.

1
  • Sure enough. I was logged in. The problem is that $facebook->getUser() can't get the user id. When I am logged out, I log in with the popup window and I am in the same situation as before. I've read this question stackoverflow.com/questions/11642214/… and I've run the second code (PHP + JS) but I can't get the user id or another info. Thanks @Viral Solani
    – Jonás
    Jul 25, 2012 at 18:45
0

To avoid that, you have to change the behaviour of your facebook script slightly with the FB.getLoginStatus. It checks if the user is logged already in Facebook and if he already authorised your app.

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        window.location = //redirect to page with logged user (you have the response token in response)
    } else {
      //Show the login popup
      FB.login(function(response) {
        if (response.authResponse) {
          window.location = //redirect to location after correct login
        }
      }, { scope: '<scopes>', state: '<state>' });
    }
  });

This way, if the user is "connected" (logged in and app authorised), you won't call the FB.login method, so the login window won't flash. In any other case, it will show the login window, which is the expected result.

Check https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus for the complete documentation.

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.