WEBProject
WEBProject

Reputation: 1345

Endless loop on fb authentication

Trying to pull out simple authentication, gives me endless loop.

strangest thing, when using Mixed js and Php api, it works.

    <?Php
//uses the PHP SDK.  Download from https://github.com/facebook/php-sdk
require 'src/facebook.php';


$facebook = new Facebook(array(
  'appId'  => '306597542762982',
  'secret' => '88XXXXXXf1',
));


if(!$facebook->getUser())
    header("location: ".$facebook->getLoginUrl ());

var_dump($facebook->getUser());

The above code gives me endless loop. BUT! if you removed the header redirection, and I use the js sdk only for the login process, it works.

Upvotes: 1

Views: 188

Answers (2)

Todd Chaffee
Todd Chaffee

Reputation: 6824

Before ANYTHING, make sure you have the latest SDK. I've seen this behavior using an outdated SDK. And the comment in your code points to a deprecated version. Make sure your code is from https://github.com/facebook/facebook-php-sdk and not from https://github.com/facebook/php-sdk as it says in your comment.

Also, you are skipping a couple of steps that you can see in the examples from Facebook on github. Try doing it exactly like they are.

$user = $facebook->getUser();
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

And then

if (!$user) {
  $loginUrl = $facebook->getLoginUrl();
  header("Location: " . $loginUrl);
}

I'm still not positive that header will work, so you might also want to try using a javascript redirect:

echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";

Upvotes: 1

Raul Pinto
Raul Pinto

Reputation: 1105

EDIT: Ok, thats not the problems solution, sorry

As far as I understand it, the header is set, but the script is not stopped. Imagine a different header (not Location), it would be bad if the script was stopped then. So try the following:

if(!$facebook->getUser()) {
    header("location: ".$facebook->getLoginUrl ());
    die();
}

HTH

Upvotes: 2

Related Questions