Error 310 (net :: ERR_TOO_MANY_REDIRECTS): Too many redirects. Using PHP SDK for facebook

Possible duplicate:
PHP-SDK has too many redirects

I am trying to save facebook login sessions to a database and use localhost as a HOST (using the facebook PHP SDK). Below is my code.

<?php

require_once 'facebook/facebook.php';
require 'config/fbconfig.php';
require 'config/functions.php';

$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            ));

$user = $facebook->getUser();

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

    if (!empty($user_profile )) {
        # User info ok? Let print it (Here we will be adding the login and     registering routines)

        $username = $user_profile['name'];
         $uid = $user_profile['id'];
           $email = $user_profile['email'];
            $user = new User();
        $userdata = $user->checkUser($uid, 'facebook',     $username,$email,$twitter_otoken,$twitter_otoken_secret);
        if(!empty($userdata)){
            session_start();
            $_SESSION['id'] = $userdata['id'];
    $_SESSION['oauth_id'] = $uid;

            $_SESSION['username'] = $userdata['username'];
    $_SESSION['email'] = $email;
            $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
            header("Location: home.php");
        }
    } else {
        # For testing purposes, if there was an error, let kill the script
        die("There was an error.");
    }
 } else {
    # There no active session, let generate one
    $login_url = $facebook->getLoginUrl(array( 'scope' => 'email'));
            header("Location: " . $login_url);
 }
?>

      

home.php looks like below ..

<?php

//Always place this code at the top of the Page
session_start();
if (!isset($_SESSION['id'])) {
    // Redirection to login page twitter or facebook
    header("location: index.php");
}
else
{
echo '<h1>Welcome</h1>';
echo 'id : ' . $_SESSION['id'];
echo '<br/>Name : ' . $_SESSION['username'];
echo '<br/>Email : ' . $_SESSION['email'];
echo '<br/>You are login with : ' . $_SESSION['oauth_provider'];
echo '<br/>Logout from <a href="logout.php?logout">' . $_SESSION['oauth_provider'] .     '</a>';
}
?>

      

But browser (chrome) is throwing below error

 Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

      

I have tried clearing all cookies but to no avail. Let me understand how to handle this error, or if there is any code that needs to be changed. Also, I tried searching the forums for bugs, but they weren't as explanatory as it might be new in this area.

+3


source to share


1 answer


The error means that there is an endless redirection loop.

It looks like index.php redirects to home.php and then home.php redirects back to index.php in an endless loop.



In your index.php and home.php, check the value $_SESSION['id']

in front of each if()

and inside each if()

as this is the main variable that will break the loop.

+2


source







All Articles