【发布时间】:2021-06-22 08:50:08
【问题描述】:
我正在尝试使用 google api for php 登录,然后将用户重定向到 dashboard.php,但在重定向期间突然发生此错误 我的文件: 负责令牌和重定向到 dashboard.php 的回调文件
<?php
session_start();
require_once("../gog.php");
if (isset($_SESSION['access_token']))
$gClient->setAccessToken($_SESSION['access_token']);
else if (isset($_GET['code'])) {
$token = $gClient->fetchAccessTokenWithAuthCode($_GET['code']);
$_SESSION['access_token'] = $token;
} else {
header('Location:../index.php');
exit();
}
$oAuth = new Google_Service_Oauth2($gClient);
$userData = $oAuth->userinfo_v2_me->get();
header('Location:../client/dashboard.php');
exit();
?>
包含设置的 gog.php(我删除了值)
<?php
require_once "vendor/autoload.php";
$gClient = new Google_Client();
$gClient->setClientId("");
$gClient->setClientSecret("");
$gClient->setApplicationName("");
$gClient->setRedirectUri("");
$gClient->addScope("");
$loginURL = $gClient->createAuthUrl();
?>
测试用户是否使用谷歌帐户或电子邮件密码登录的索引 php
<?php
require_once("includes/config.php");
require_once("gog.php");
require_once("includes/classes/Account.php");
$account=new Account($con);
if(isset($_SESSION["clientLoggedIn"]) || isset($_SESSION['access_token']) ){
header('location:client/dashboard.php');
exit();
}
?>
包含要连接的数据库设置的 config.php 文件
<?php
ob_start();
session_start();
try{
$con= new PDO("mysql:dbname=cinecad;host=localhost","root","");
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}catch(PDOException $e){
exit("Connexion failed:".$e->getMessage());
}
?>
最后是 dashbaord.php
<?php
session_start();
if(!isset($_SESSION["clientLoggedIn"])||!isset($_SESSION['access_token'])){
header("Location:../index.php");
}
?>
【问题讨论】:
-
错误说明了什么?也请向我们展示错误。
-
问题出在页面未正确重定向,尝试使用gmail帐户登录后重定向用户
-
请阅读header 文档。我相信不支持相对 URL。不是
../index.php,而是/index.php应该可以工作。 -
按照这个逻辑,你不需要在
$_SESSION['access_token'] = $token;之后添加$_SESSION["clientLoggedIn"] = true;,还是在header('Location:../client/dashboard.php');之前添加$_SESSION["clientLoggedIn"] = true;? -
@hppycoder 嗨,不确定您是否正确
标签: php