我试图将facebook登录会话保存到数据库并使用localhost作为HOST(使用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,但无济于事。让我了解如何处理此错误,或者是否有要更改的代码。此外,我试图在论坛中查找错误,但它们并没有那么解释,因为它可能是这个领域的新手。
答案 0 :(得分:2)
错误意味着存在无限循环的重定向。
看起来像index.php重定向到home.php,然后home.php重定向回index.php,在无限循环中..
在index.php和home.php中,检查每个$_SESSION['id']
之前和if()
内的if()
的值,因为它是打破循环的主要变量。< / p>