Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Google OAuth2在重定向到另一个页面时是否破坏会话?_Php_Session_Oauth_Oauth 2.0_Google Oauth - Fatal编程技术网

Php Google OAuth2在重定向到另一个页面时是否破坏会话?

Php Google OAuth2在重定向到另一个页面时是否破坏会话?,php,session,oauth,oauth-2.0,google-oauth,Php,Session,Oauth,Oauth 2.0,Google Oauth,我正在尝试使用Google OAuth2在我的网站上实现登录功能。 如果我在同一页中选中一个$\u会话,它包含我设置的所有变量,但当我重定向到另一页时,$\u会话没有任何变量。为什么? Register.php <?PHP require './google/setup.php'; session_start(); $client = new apiClient(); $client->setClientId($client_id); $client->setClientSe

我正在尝试使用Google OAuth2在我的网站上实现
登录
功能。 如果我在同一页中选中一个
$\u会话
,它包含我设置的所有变量,但当我重定向到另一页时,
$\u会话
没有任何变量。为什么?

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>';
}
?>

我最近遇到了类似的问题。让它绊倒的是我正在重定向到
http://myserver.com
而不是
http://www.myserver.com
。我来自
http://www.myserver.com
。也许这就是你面临的问题?我将检验你的建议。我希望你是对的。
<?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;
?>