Your problem is that you are setting a redirect header but then not following thru.
This section of code is executes:
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));
}
but instead of exiting the PHP after you set the Location
header you continue on:
if(isset($_SESSION['access_token']) && $_SESSION['access_token']){
.....
}
and then finally this code executes:
else{
$authUrl = $client->createAuthUrl();
}
SOLUTION:
Change to this block of code: notice the addition of 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();
}
Suggestion:
If you plan to send HTTP headers to the client, always add ob_start();
at the beginning of your PHP code. This turns on output buffering. This prevents output from being sent before your headers. Follow up with ob_end_flush();
before your code exits.
<?php
ob_start();
Enable error reporting. At the top of your PHP file add (plus my other suggestion):
<?php
// enable output buffering
ob_start();
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
Also, if you have PHP error logging setup, you will be able to see errors and warning. I will bet there are a few that need to be fixed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…