php - 为什么我的网站在 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();
?>
请帮助我..谢谢。
解决方案
您的问题是您正在设置重定向标头,但随后没有通过。
这部分代码执行:
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 错误日志记录,您将能够看到错误和警告。我敢打赌有一些需要修复。
推荐阅读
- swift - CloudKit中的Core Data发生变化时如何调用WidgetCenter.shared.reloadAllTimelines()?
- python - 风筝 vs 绝地,排名并适合 python 新手
- angular - “用户正在打字”精益 RXJS 实现
- tensorflow - 使用对象检测 api 训练 tensorflow 1 模型
- url - Wikipedia url,是否需要英语语言代码才能在 2 种语言之间进行跨维基链接?
- java - Android Room:错误:setValue(T) 在 LiveData 中具有受保护的访问权限
- javascript - 如何在 javascript 中生成基于 2 或 3 个单词的描述?
- mongodb - Logstash + MongoDB Atlas 输出:无法与 xxx.gcp.mongodb.net:27017 握手:ArgumentError:参数数量错误(给定 2,预期为 1)
- android - 查询子集合的字段并在firestore android中获取父DocumentSnapshot
- algorithm - 在线性时间内找到最长的平衡括号子序列