我正在尝试使用Google OAuth2在我的网站上实现sign in
功能。
如果我检查同一页面中的$_SESSION
它包含我设置的所有变量,但是当我重定向到另一个页面时,$_SESSION
没有任何变量。为什么呢?
Register.php
<?PHP
require './google/setup.php';
session_start();
$client = new apiClient();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_url);
$client->setApprovalPrompt(false);
$oauth2 = new apiOauth2Service($client);
if (isset($_GET['code']))
{
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
unset($_SESSION['google_data']); //Google session data unset
$client->revokeToken();
}
if ($client->getAccessToken())
{
$user = $oauth2->userinfo->get();
$_SESSION['google_data']=$user; // Storing Google User Data in Session
//$_SESSION['google_data']['name'] //IT HAS A VALUE!!!
header("location: ../session.php"); //redirect to session.php to check SESSION'S variables
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}
if(isset($authUrl))
{
echo '<a class="login" href="'.$authUrl.'">Google Account Login</a>';
}
?>
session.php文件:
<?php
require_once("authorization/include/membersite_config.php");
session_start();
$userdata=$_SESSION['google_data'];
echo 'VARIABLES: '.$userdata['email'].' '.$userdata['name']; //IT DOESN'T HAVE ANY VALUES!
exit;
?>
它返回空!我做错了什么?