php页面中的网页重定向循环错误

时间:2014-10-15 09:54:48

标签: php redirect google-login

我在索引页面中使用谷歌登录以及正常登录。 但是,当我通过Google登录时,它会显示“这个网页有一个重定向循环'在chrome和firefox中。 在我的代码中,我已根据特定条件将这么多标题重定向到特定页面。但现在当我加载页面时,它显示重定向循环错误。

任何人都可以帮我找到我的代码有什么问题。我的逻辑中找不到任何错误。

这是我的代码:

Google API代码:

<?php
require_once 'google-login-api/src/Google_Client.php';
require_once 'google-login-api/src/contrib/Google_Oauth2Service.php';
$google_client_id       = ''; //client id
$google_client_secret   = ''; //client secret
$google_redirect_url    = ''; //redirect url
$gClient = new Google_Client();
$gClient->setClientId($google_client_id);
$gClient->setClientSecret($google_client_secret);
$gClient->setRedirectUri($google_redirect_url);

$google_oauthV2 = new Google_Oauth2Service($gClient);
 if (isset($_GET['code'])) 
{ 
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL));
return;
}
if (isset($_SESSION['token'])) 
{ 
$gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) 
{
  //For logged in user, get details from google using access token
  $user                 = $google_oauthV2->userinfo->get();
  $user_name            = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
  $email                = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
  $profile_image_url    = filter_var($user['picture'], FILTER_VALIDATE_URL);
  $_SESSION['token']    = $gClient->getAccessToken();
}
else 
{
//For Guest user, get google login url
$authUrl = $gClient->createAuthUrl();
}
?>

用于处理谷歌登录的PHP代码:

<?php
if(isset($authUrl)) //user is not logged in, show login button
{
echo '<a class="login" href="'.$authUrl.'"><img src="google-login-api/images/google-login-button.png" /></a>';
} 
else // user logged in 
{
$_SESSION['gemail']=$email;
$_SESSION['guname']=$user_name;
$glogqry="select user_id from tab_user_login where username='$email' and type='Developer'";
$reslog=mysql_query($glogqry);
$countlog=mysql_num_rows($reslog);
if($countlog>0)
{
$rowlog=mysql_fetch_array($reslog);
$gdevid=$rowlog[0];
$sqlapp="select * from tab_app where user_id='$gdevid' order by date limit 1";
$resapp=mysql_query($sqlapp);
$countapp=mysql_num_rows($resapp);
if($countapp<=0)
{
header('Location:home_demo.php');
}
else
{
$rowapp=mysql_fetch_array($resapp);
if(($rowapp[9]=="Test") || ($rowapp[11]=="Not Integrated"))
{
header('Location:home_demo.php');
}
else
{
header('Location:home.php');
}
}
}
else
{
$glogqry1="insert into tab_user(name,email,image,signup_date)        values('$user_name','$email','$profile_image_url','$today')";
$reslog1=mysql_query($glogqry1);
if($reslog1!=0)
{
$devid=mysql_insert_id();
}
$glogqry12="insert into tab_user_login (user_id,username,type,status,login_status) values('$devid','$email','Developer','Approved','first login')";
$reslog12=mysql_query($glogqry12);
header('Location:home_demo.php');
}
}
?>

任何人都可以帮我这个.. ??我被困在这两天以上..

先谢谢

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。? 我把代码用于处理另一个页面中的google登录,这是重定向网址。在该页面中,我根据特定条件将标题重定向到特定页面。

现在我的代码是:

我的索引页

<?php
ob_start();
session_start();
include("db.php");
include("function.php");
include("sendemail.php");

require_once 'google-login-api/src/Google_Client.php';
require_once 'google-login-api/src/contrib/Google_Oauth2Service.php';

$google_client_id       = ''; //client id
$google_client_secret   = ''; // client secret
$google_redirect_url    = ''; //redirect url
$gClient = new Google_Client();
$gClient->setClientId($google_client_id);
$gClient->setClientSecret($google_client_secret);
$gClient->setRedirectUri($google_redirect_url);

$google_oauthV2 = new Google_Oauth2Service($gClient);

$authUrl = $gClient->createAuthUrl();

/* here comes the code for normal log in */

if(isset($authUrl)) //user is not logged in, show login button
{
    echo '<a class="login" href="'.$authUrl.'"><img src="google-login-api/images/google-login-button.png" /></a>';
}

/*Here comes the code to handle normal log in*/
?>

处理Google注册的代码:

<?php
ob_start();
session_start();

include("db.php");
include("function.php");
require_once 'google-login-api/src/Google_Client.php';
require_once 'google-login-api/src/contrib/Google_Oauth2Service.php';

session_start();

$google_client_id       = '';
$google_client_secret   = '';
$google_redirect_url    = '';
$gClient = new Google_Client();
$gClient->setClientId($google_client_id);
$gClient->setClientSecret($google_client_secret);
$gClient->setRedirectUri($google_redirect_url);

$google_oauthV2 = new Google_Oauth2Service($gClient);
if (isset($_GET['code'])) 
{ 
    $gClient->authenticate($_GET['code']);
    $_SESSION['token'] = $gClient->getAccessToken();
}
if (isset($_SESSION['token'])) 
{ 
    $gClient->setAccessToken($_SESSION['token']);
}
if ($gClient->getAccessToken()) 
{
    $user               = $google_oauthV2->userinfo->get();
    $user_name          = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
    $email              = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
    $profile_image_url  = filter_var($user['picture'], FILTER_VALIDATE_URL);
    $_SESSION['token']  = $gClient->getAccessToken();

    $_SESSION['gemail']=$email;
    $_SESSION['guname']=$user_name;
    $today=date('y-m-d H:i:s');
    $glogqry="select user_id from tab_user_login where username='$email' and type='Developer'";
    $reslog=mysql_query($glogqry);
    $countlog=mysql_num_rows($reslog);
    if($countlog>0)
    {
        $rowlog=mysql_fetch_array($reslog);
        $gdevid=$rowlog[0];
        $sqlapp="select * from tab_app where user_id='$gdevid' order by date limit 1";
        $resapp=mysql_query($sqlapp);
        $countapp=mysql_num_rows($resapp);
        if($countapp<=0)
        {
            header('Location:home_demo.php') or die("regstd, no app ");
        }
        else
        {
            $rowapp=mysql_fetch_array($resapp);
            if(($rowapp[9]=="Test") || ($rowapp[11]=="Not Integrated"))
            {
                header('Location:home_demo.php')or die("not verified or integtd");
            }
            else if(($rowapp[9]=="Not Approved") && ($rowapp[11]=="Integrated"))
            {
                header('Location:home_demo.php')or die("success");
            }
            else if($rowapp[9]=="Approved")
            {
                header('Location:home.php');
            }
        }
    }
    else
    {
        $glogqry1="insert into tab_user(name,email,image,signup_date)  values('$user_name','$email','$profile_image_url','$today')";
        $reslog1=mysql_query($glogqry1);
        if($reslog1!=0)
        {
            $devid=mysql_insert_id();
        }
        $glogqry12="insert into tab_user_login (user_id,username,type,status,login_status) values('$devid','$email','Developer','Approved','first login')";
        $reslog12=mysql_query($glogqry12);
        $glogbreak="insert into tab_user_break_points (dev_id,signup,add_app,app_id,download_sdk,integrate_sdk,verify_app,approve_app,) values('$devid','1','not done','not done','not done','not done','not done','not done')";
        $reslogbreak=mysql_query($glogbreak);
        header('Location:home_demo.php')or die("new user");

    }
}

?>

现在它运作正常。