【问题标题】:How to fix The page isn’t redirecting properly如何修复页面未正确重定向
【发布时间】: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


【解决方案1】:

根据@Alon Eitan 的评论,答案是 $_SESSION["clientLoggedIn"] = true

【讨论】:

  • 虽然我们感谢您回答自己的问题,但为什么要解决这个问题?除了从 cmets 那里得到答案之外,这对解决您的问题有何帮助?您必须实现的最终代码是什么?
猜你喜欢
  • 2017-01-18
  • 2011-08-11
  • 2018-06-08
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多