3

可能重复:
PHP-SDK 重定向过多

我正在尝试将 facebook 登录会话保存到数据库并使用 localhost 作为主机(使用 PHP SDK for facebook)。下面是我的代码。

<?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's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }

    if (!empty($user_profile )) {
        # User info ok? Let's 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's kill the script
        die("There was an error.");
    }
 } else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl(array( 'scope' => 'email'));
            header("Location: " . $login_url);
 }
?>

home.php 如下所示..

<?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>';
}
?>

但是浏览器(chrome)抛出错误

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

我已尝试清除所有 cookie 但无济于事。让我了解如何处理此错误,或者是否需要更改代码。我也尝试在论坛中查找错误,但它们并不是那么解释,因为它可能是这个领域的新手。

4

1 回答 1

2

该错误意味着存在无限循环的重定向。

似乎 index.php 正在重定向到 home.php,然后 home.php 在无限循环中重定向回 index.php..

在您的 index.php 和 home.php 中,检查$_SESSION['id']each 之前if()和内部 each的值if(),因为它是破坏循环的主要变量。

于 2013-01-27T09:37:50.877 回答