1

I'm working on a local Facebook project. But when I go to localhost, he starts redirecting to localhost/?code=xxxx everytime (xxxx is very long random characters).

My code is:

require_once('sdk/src/facebook.php');

$facebook = new Facebook(array(
  'appId'  => 'MY_APP_ID',
  'secret' => 'MY_APP_SECRET',
  'sharedSession' => true,
  'trustForwarded' => true,
  'cookie' => true
));

$user_id = $facebook->getUser();
if ($user_id) {
  try {
    // Fetch the viewer's basic information
    $basic = $facebook->api('/me','GET');
  } catch (FacebookApiException $e) {
    if (!$facebook->getUser()) {
      //header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
      $user_id = null;
    }
  }
}

if(!$user_id) {
    $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'email,publish_actions'
  ));
    
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
      exit();
}
...

My app settings:

Thanks in advance.

3
  • That might indicate that your session is lost – check if your browser accepts PHP’s session cookies for your localhost correctly.
    – CBroe
    Aug 13, 2013 at 13:48
  • @CBroe My Chrome browser doesn't accept cookies, but Firefox do. And unfortunately, redirect loop is happening on all my browsers. What can I do?
    – mhmtemnacr
    Aug 13, 2013 at 14:45
  • 1
    Also try removing sharedSession and trustForwarded parameters when initializing the SDK, if you don’t really need them.
    – CBroe
    Aug 13, 2013 at 14:58

1 Answer 1

1

The likely issue is that your app is returning 0 for $user_id even after the user has logged in, causing the JavaScript redirect to happen again, causing it to loop forever.

The case of the issue may be one of many things, but make sure the code GET variable isn't being stripped out of the URL by a .htaccess file or other means. Secondly, take a look at this modification to the base_facebook.php file to change the way the code is checked.

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.