首页 > 解决方案 > 为什么我的网站在 Google 登录后没有重定向

问题描述

我有一个网络应用程序,要求用户使用他们的谷歌帐户登录。但是,在我登录后,它不会将我重定向到另一个页面。它停留在登录页面。

登录前:

在此处输入图像描述

登录后:

在此处输入图像描述

我试过使用 php Header() 函数,但它没有重定向我。我错过了什么吗?

在此处输入图像描述

<?php
ob_start();
session_start();

require_once 'google-api-php-client-2.2.2/vendor/autoload.php';


$conn = new mysqli("localhost","root","","labelimagetool");
if($conn->connect_error){
    die("Connection failed: ".$conn->connect_error);
    echo 'Unable to connect to db!';
}
else{
    echo 'Connected to db!';
}

$redirect_uri = 'http://localhost/labelimagetool/tool.php';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setDeveloperKey($api_key);
$client->setRedirectUri($redirect_uri);
//$service = new Google_Service_Oauth2($client);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$client->setAccessType('offline');        // offline access
$client->setIncludeGrantedScopes(true); 
//$client->authenticate(isset($_GET['code']));
//$client->authenticate($code); 

$plus = new Google_Service_Plus($client);

if(isset($_REQUEST['logout'])){
    session_unset();
}
if(isset($_GET['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $redirect = 'http://'.$_SERVER['HTTP_HOST'].'/tool.php';
    header('Location:'.filter_var($redirect,FILTER_SANITIZE_URL));  
    exit();
}
else{
    echo "no value";
}

if(isset($_SESSION['access_token']) && $_SESSION['access_token']){

    $client->setAccessToken($_SESSION['access_token']);
    $me = $plus->people->get('me');

    $email = $me['emails'][0]['value'];
    $name = $me['displayName'];
    $sqlEmail = "SELECT * FROM users WHERE email='".$email."'";
    $checkEmail = mysqli_query($conn,$sqlEmail);
    if(mysqli_num_rows($checkEmail) >0){

    }
    else{
    $insertStmt = "INSERT INTO users ('email','name','status') VALUES('".$email."','".$name."','user')";
    mysqli_query($conn,$insertStmt);
    }
}
else{
    $authUrl = $client->createAuthUrl();
}  
ob_end_flush();
?>

请帮助我..谢谢。

标签: phpoauthgoogle-apigoogle-oauthgoogle-api-php-client

解决方案


您的问题是您正在设置重定向标头,但随后没有通过。

这部分代码执行:

if(isset($_GET['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $redirect = 'http://'.$_server['HTTP_HOST'].'/tool.php';
    header('Location:'.filter_var($redirect,FILTER_SANITIZE_URL));
}

但不是在设置Location标题后退出 PHP,而是继续:

if(isset($_SESSION['access_token']) && $_SESSION['access_token']){
    .....
}

然后最后这段代码执行:

else{
    $authUrl = $client->createAuthUrl();
}

解决方案:

更改此代码块:注意添加exit()

if(isset($_GET['code'])){
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    $redirect = 'http://'.$_server['HTTP_HOST'].'/tool.php';
    header('Location:'.filter_var($redirect,FILTER_SANITIZE_URL));
    exit();
}

建议

如果您计划向客户端发送 HTTP 标头,请始终ob_start();在 PHP 代码的开头添加。这将打开输出缓冲。这可以防止在您的标头之前发送输出。ob_end_flush();在您的代码退出之前跟进。

<?php
ob_start();

启用错误报告。在您的 PHP 文件顶部添加(加上我的其他建议):

<?php
// enable output buffering
ob_start();
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

此外,如果您设置了 PHP 错误日志记录,您将能够看到错误和警告。我敢打赌有一些需要修复。


推荐阅读