Facebook PHP SDK: getUser returns 0 on first call

The problem: a facebook login doesn’t always work. Although the user has authenticated the app, getUser returns 0.

So, the user clicks a LOGIN button and is being redirected to $facebook->getLoginUrl. Facebook forces the user to authenticate the app and afterwards redirects back to my page, to login.php to be exact. And this is where I call getUser, expecting to get the current user’s facebookUserId. But this didn’t work for a fresh user who hasn’t been to my site just yet. What my script now did was to redirect back to getLoginUrl, which in turn redirects back to login.php and so on, which quickly makes the browsers stop and claim a “redirect error” (gee, I hope that’s the right translation..). By the way, chrome is the first to give in, even before IE. Scary.

The solution: a javascript redirect. Instead of doing a php redirect, I show a page which most users shouldnt even see and on this page there’s a location.href, leading back to the getLoginUrl. And, strangely enough, that works flawlessly!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.