Google 登录 PHP 未在身份验证后重定向

问题描述

我遇到了一个问题,在我对用户进行身份验证后,它再次将我重定向登录页面,而不是 home-page.PHP,它应该显示用户的 google 登录信息。我尝试添加一个 exit();像此线程 (Why is my web not redirecting after Google Login) 建议的那样进行身份验证后,但我仍然遇到相同的问题。

Config.PHP

<?PHP

// Google Api configuration
define('GOOGLE_CLIENT_ID','659848906740-qvoftv2sg9urfh5kt5a2hh3mcel57hsa.apps.googleusercontent.com');
define('GOOGLE_CLIENT_SECRET','Te3sltN31StU3zgmUQm0SBwR');
define('GOOGLE_REDIRECT_URL','https://cgi.sice.indiana.edu/~team03/team-03/Server/profile_log.PHP');

// Start session
if(!session_id()){
    session_start();
}

// Include Google Api client library
require_once 'google-api-PHP-client/Google_Client.PHP';
require_once 'google-api-PHP-client/contrib/Google_Oauth2Service.PHP';

// Call Google Api
$gClient = new Google_Client();
$gClient->setApplicationName('Login to Hair Identity');
$gClient->setClientId(GOOGLE_CLIENT_ID);
$gClient->setClientSecret(GOOGLE_CLIENT_SECRET);
$gClient->setRedirectUri(GOOGLE_REDIRECT_URL);

$google_oauthV2 = new Google_Oauth2Service($gClient);

?>

index.PHP

<?PHP 
 // Include configuration file 
 require_once 'config.PHP'; 
 session_start();

 if(isset($_GET['code'])){ 
     $gClient->authenticate($_GET['code']); 
     $_SESSION['token'] = $gClient->getAccesstoken(); 
     header('Location: ' . filter_var(GOOGLE_REDIRECT_URL,FILTER_SANITIZE_URL)); 
 } 

 if(isset($_SESSION['token'])){ 
     $gClient->setAccesstoken($_SESSION['token']); 
 } 
 

if($gClient->getAccesstoken()){ 
     // Get user profile data from google 
     $gProfile = $google_oauthV2->userinfo->get(); 

     // Getting user profile info 
     $gData = array(); 
     $gData['oauth_uid']  = !empty($gProfile['id'])?$gProfile['id']:''; 
     $gData['first_name'] = !empty($gProfile['given_name'])?$gProfile['given_name']:''; 
     $gData['last_name']  = !empty($gProfile['family_name'])?$gProfile['family_name']:''; 
     $gData['email']      = !empty($gProfile['email'])?$gProfile['email']:''; 
     $gData['gender']     = !empty($gProfile['gender'])?$gProfile['gender']:''; 
     $gData['locale']     = !empty($gProfile['locale'])?$gProfile['locale']:''; 
     $gData['picture']    = !empty($gProfile['picture'])?$gProfile['picture']:''; 

     $userData = $gData; 

     // Storing user data in the session 
     $_SESSION['userData'] = $userData; 

     // storing user profile data in session
     if(!empty($userData)){ 

     $_SESSION["upic"]= '<img width="100" src="'.$userData['picture'].'">'; 
     $_SESSION["uid"]= '<p><b>Google ID:</b> '.$userData['oauth_uid'].'</p>'; 
     $_SESSION["uname"]= '<p><b>Name:</b> '.$userData['first_name'].' '.$userData['last_name'].'</p>'; 
     $_SESSION["uemail"]= '<p><b>Email:</b> '.$userData['email'].'</p>'; 
     $_SESSION["logout"]= '<p><a href="logout.PHP"><b>logout</b></a></p>'; 

     header("Location: home-page.PHP");


     }else{ 
         $out_error = '<h3 style="color:red">Some problem occurred,please try again.</h3>'; 
         echo $out_error;
     } 
 }else{ 
    // Get login url 
    $authUrl = $gClient->createAuthUrl(); 

    echo "<center>";
    echo "<h1>Continue logging in with Google </h1>";
    // google login button 
    $login_image = '<a href="'.filter_var($authUrl,FILTER_SANITIZE_URL).'">
    <img src="images/google-sign-in-btn.png" alt=""/></a>'; 
    echo $login_image;
    echo "</center>";
 } 
 ?> 
 
 <html>
    <head>
        <link rel="stylesheet" href="login.css">
    </head>
    <body>
    
    </body>
 </html>

home-page.PHP

<!DOCTYPE html>
<html>
<head>
<title>Google Account</title>
</head>
<body>
<center>
<?PHP 

session_start();

if(isset($_SESSION["uname"])){ 
    
 echo "<h1>Your Google Details </h1>";

 echo   $_SESSION["upic"];
 echo   $_SESSION["uid"];
 echo   $_SESSION["uname"];
 echo   $_SESSION["uemail"];
 echo   $_SESSION["logout"];
    
}else{
    
    header("index.PHP");
}

?>
</center>
</body>

logout.PHP

<?PHP
// Include configuration file
require_once 'config.PHP';

// Remove token and user data from the session
unset($_SESSION['token']);
unset($_SESSION['userData']);

// Reset OAuth access token
$gClient->revoketoken();

// Destroy entire session data
session_destroy();

// Redirect to homepage
header("Location:index.PHP");
?>

解决方法

来自documentation

注意:

大多数当代客户端都接受相对 URI 作为 » 位置:,但一些较老的客户端需要绝对 URI,包括 方案、主机名和绝对路径。你通常可以使用 $_SERVER['HTTP_HOST'],$_SERVER['PHP_SELF'] 和 dirname() 来自您自己的相对 URI 的绝对 URI:

<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>

试试这个。

,

index.php 中的 header("Location: home-page.php"); 代码是正确的,应该是您希望重定向到的页面,在这种情况下,我的是显示用户 google 信息的页面。问题出在 config.php 中,其中您的 Google_redirect_URL 应该是您的 google 按钮连接到的文件,在我的情况下是 index.php。您还需要将该网址添加到您的 Google 控制台,以免出现不匹配错误。