我在索引页面中使用谷歌登录和普通登录。但当我通过谷歌登录时,chrome和firefox都显示"这个网页有一个重定向循环"。在我的代码中,我放了很多标题,以便根据特定的条件重定向到特定的页面。但现在,当我加载页面时,它显示重定向循环错误。
有人能帮我找出我的代码出了什么问题吗。我在逻辑上找不到任何错误。
这是我的代码:
谷歌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');
}
}
?>
有人能帮我吗。。??我被这件事困扰了两天多。。
提前感谢
我找到了解决方案。?我把处理谷歌登录的代码放在另一个页面上,这个页面就是重定向url。在那个页面上,我放置了标题,以便根据特定条件重定向到特定页面。
现在我的代码是:
我的索引页面
<?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*/
?>
处理谷歌注册的代码:
<?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");
}
}
?>
现在它运行良好。