0

I am trying to login with google api for php and then redirect the user to the dashboard.php but suddenly this error occurred during the redirection my files: the callback file that is responsible for tokens and redirection to 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();


?>

the gog.php which includes the settings (i deleted the values)

<?php
    require_once "vendor/autoload.php";
    $gClient = new Google_Client();
    $gClient->setClientId("");
    $gClient->setClientSecret("");
    $gClient->setApplicationName("");
    $gClient->setRedirectUri("");
    $gClient->addScope("");
    $loginURL = $gClient->createAuthUrl();


?>

the index php where it testes wether or not the user is signed in with google account or email pwd

<?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();
}
?>

the config.php file included which has the database setting to connect

<?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());
}

?>

and the finally the dashbaord.php

<?php
session_start();

if(!isset($_SESSION["clientLoggedIn"])||!isset($_SESSION['access_token'])){

    header("Location:../index.php");
}



?>
  • What does the error say? Please also show us the error as well. – FerdousTheWebCoder Mar 25 '21 at 14:23
  • It's in the question The page isn’t redirecting properly , trying to redirect the user after logging with gmail account – souid chaima Mar 25 '21 at 14:24
  • Please read the [header](https://www.php.net/manual/en/function.header.php) documentation. I believe that relative URLs aren't supported. Not a `../index.php` but instead `/index.php` should work. – hppycoder Mar 25 '21 at 14:29
  • By this logic, don't you need to add `$_SESSION["clientLoggedIn"] = true;` after `$_SESSION['access_token'] = $token;`, or just before `header('Location:../client/dashboard.php');`? – Alon Eitan Mar 25 '21 at 14:43
  • @hppycoder Hi, Not sure you are right there – RiggsFolly Mar 25 '21 at 14:47
  • You're probably right @RiggsFolly... It's been a while. I used this https://stackoverflow.com/questions/10541439/header-location-relative-path-compatibility as a guide though which indicated to me it needs more than the `../` which should walk back a folder but can't as it's a location redirect – hppycoder Mar 25 '21 at 14:50
  • @hppycoder I was not certain so I tried it :) – RiggsFolly Mar 25 '21 at 14:55

1 Answers1

1

Based on @Alon Eitan comment the answer was $_SESSION["clientLoggedIn"] = true

  • While we appreciate you answering your own question why did it fix that? Besides taking the answer from the comments how did that help solve your issue? What was the final code that you had to implement? – hppycoder Mar 25 '21 at 17:46