5

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?

Hassen
  • 6,966
  • 13
  • 45
  • 65
  • Have you enabled cookies in your browser? – talha2k May 13 '12 at 16:59
  • Yes, of course. I use the setting that allow local data to be set (recommended). – Hassen May 13 '12 at 17:25
  • strange then. place an exit after $loginURL = $facebook->getLoginURL(); and then update if it still redirects. – talha2k May 13 '12 at 17:27
  • I updated my code. Now I just display the loginUrl instead of redirecting automatically. `$loginURL = $facebook->getLoginURL(); echo $loginURL; // <-- HERE I CHANGED THE CODE TO DISPLAY LOGIN URL` But it still doesn't work!!! – Hassen May 13 '12 at 17:32
  • Like $loginURL = $facebook->getLoginURL(); exit; – talha2k May 13 '12 at 17:35
  • Unfortunately, it still doesn't work (I placed exit() after $loginURL). This gives me the login URL to click on. When I click, it redirects me to my script with (again) the login URL... – Hassen May 13 '12 at 17:47
  • The given URL to click on is `https://www.facebook.com/dialog/oauth?client_id=209633612480053&redirect_uri=http%3A%2F%2Fwww.bluward.com%2Foauth%2Ffacebook&state=29f22cd0f394ff591b7d765b11d759e1`. – Hassen May 13 '12 at 17:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11202/discussion-between-alphamale-and-hassinus) – talha2k May 13 '12 at 17:53
  • Were you able to solve the issue? – Roel Veldhuizen Oct 25 '12 at 08:31
  • I don't know if it can be usefull to you, but i made a module for kohana while ago, https://github.com/Faianca/Facebook-Connect and it was working properly. check it out – Jorge Faianca Jan 14 '14 at 20:59

2 Answers2

5

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.

viktike
  • 717
  • 4
  • 7
0

I had a similar issue with chrome browser, cookies always were incorrect or empty.

Then I downloaded a facebook example from Heroku (it uses a popup window as login instead of a js redirect) and noticed that it doesn't work in chrome when the base URL of the website isn't the server root URL (ex: localhost/somedirectory/). But after creating a virtual host it looks like it works.

You can see an example of the index.php from the application here https://gist.github.com/2845986

thanksd
  • 54,176
  • 22
  • 157
  • 150
Fivell
  • 11,829
  • 3
  • 61
  • 99