Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook-connect gives a redirect loop

Please, I need help. I'm dealing with this issue for 1 month!!

I would like to implement facebook connect login to my website, using PHP and php-sdk 3.1.1. In few words, my code works offline (on localhost) but not online which results in "Too many redirect loop (on Chrome)": Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

Here is my code:

1/ I load facebook connect SDK and init it:

    require 'src/facebook.php';
    $facebook = new Facebook(array(
        'appId'  => '209633612480053',
        'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    ));

Please, note that I created two apps on facebook-developper page, one for offline tests, and the other for online tests. And I'm sure to switch correctly betwwen the two pairs of appId/secret (online and offline) when testing. So it's not the problem of bad facebbok-connect init.

2/ I try to get user info:

  $uid = $facebook->getUser();

  if($uid)
  {
     /*
      * Get user information.
      */
     $user = $facebook->api('me/');
     print_r($user); // Display user info.
  }
  else
  {
     /*
      * Redirect to FB login URL to allow access.
      */
     $loginURL = $facebook->getLoginURL();
     echo '<script> top.location.href=\''.$loginURL.'\'</script>';
  }

It's as simple as that: If user ic connected to facebook, so display it's information, else, redirect to facebook login page to allow access.

IT WORKS PERFECTLY OFFLINE, but online, I get a chrome error:

This webpage has a redirect loop
The webpage at https://www.facebook.com/dialog/oauth?client_id=209633612480053&redirect_uri=http%3A%2F%2Fwww.bluward.com%2Foauth%2Ffacebook&state=551f60cd4be6cd8ed1622f8168a5219a#_=_ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

Some additional information: Online, I use 1and1 host provider, and to be sure to have the same server configuration than offline server (which is a MAMP Pro), I uploaded the sams php.ini file.

Please, if someone has an idea, or get a similar problem, I'll be glad to have help.

Thank you in advance for all help.

UPDATE:

I updated my code to focus on the problematic line, so instead of redirecting to facebook login page, I display the redirect URL, so I just have to click to login:

  $uid = $facebook->getUser();

  if($uid)
  {
     /*
      * Get user information.
      */
     $user = $facebook->api('me/');
     print_r($user); // Display user info.
  }
  else
  {
     /*
      * Redirect to FB login URL to allow access.
      */
     $loginURL = $facebook->getLoginURL();
     echo $loginURL; // <-- HERE I CHANGED THE CODE TO DISPLAY LOGIN URL
  }

What I noticed is that facebook is infinitely redirecting to my script page. Only code parameter on URL bar changes.

So, why facebbok is redirecting to my script page without giving me user info?

Please, any idea?

like image 446
htaidirt Avatar asked May 13 '12 16:05

htaidirt


People also ask

How do I fix the redirect loop on Facebook?

To fix this problem, clear your browser cache, disable your extensions, and make sure your date and time settings are correct. If the issue persists, reinstall your browser or switch to a different web browser.

Why does my Facebook say redirecting?

If you're being redirected to an error page on Facebook, this usually means that Facebook is undergoing maintenance or a glitch, and you may need to wait for a few hours to regain access to your account. You'll also be redirected to an error page if your account has been closed or terminated due to abuse.

What is a redirect loop?

A redirect loop occurs when a URL is redirected to another URL, which in turn redirects back to the URL that was originally requested, leading to an infinite cycle of redirects. You could say that a redirect loop is a closed chain of redirects.


1 Answers

I had this problem, in my case I had to edit base_facebook.php from the SDK:

public static $CURL_OPTS = array(
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_USERAGENT      => 'facebook-php-3.2',
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
);

The last two options I added manually.

like image 103
viktike Avatar answered Oct 11 '22 12:10

viktike