我不知道为什么会话数据在不同页面中无法访问。在下面的代码中,我创建了一个名为“google_data”的会话来存储Google用户的数据。尝试在denied.php中访问该会话时,会话数据不可用。有人帮帮我吗?
googleauthn.php
**<?php
//require_once 'src/apiClient.php';
//require_once 'src/contrib/apiOauth2Service.php';
session_start();
require_once dirname(__FILE__).'\vendor\autoload.php';
require_once dirname(__FILE__).'\vendor\google\apiclient\src\Google\Service\Oauth2.php';
require_once dirname(__FILE__).'\vendor\google\apiclient\src\Google\Client.php';
$client_id = '';//'myclient id';
$client_secret = ''; // 'secretkey';
$redirect_uri = 'http://localhost:81/google/denied.php';
$simple_api_key =''; //my api key;
$client = new Google_Client();
$client->setApplicationName("Google Account Login");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$oauth2 = new Google_Service_Oauth2($client);
if (isset($_GET['code']))
{
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
//$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
else{
//header('Location:http://localhost:81/google/googleauthn.php');
}
if (isset($_SESSION['token']) && $_SESSION['token']) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
unset($_SESSION['google_data']); //Google session data unset
$client->revokeToken();
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
if ($client->getAccessToken())
{
$user = $oauth2->userinfo->get();
$_SESSION['google_data']=$user; // Storing Google User Data in Session
$_SESSION['token'] = $client->getAccessToken();
header('location: '.filter_var($redirect, FILTER_SANITIZE_URL));
}
else {
$authUrl = $client->createAuthUrl();
}
/* if(isset($personMarkup)):
print $personMarkup;
endif */
if(isset($authUrl))
{
echo "<a class=login href=$authUrl>Google Account Login</a>";
}
else {
echo "<a class='logout' href='?logout'>Logout</a>";
}
?>**
denied.php
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//include('googleauthn.php');
session_start();
$host='localhost';
$db='dummy';
$user='root';
$password='';
print_r ($_SESSION['google_data']);
$conn =new mysqli($host, $user, $password,$db);
if($conn->connect_error)
{
die('could not connet db'. mysql_error());
}
else
{
if(!isset($_SESSION['google_data'])) {
// Redirection to application home page.
//header("location: index.php");
echo 'bad session';
}
else
{
//echo print_r($userdata);
$userdata=$_SESSION['google_data'];
$email =$userdata['email'];
$googleid =$userdata['id'];
$fullName =$userdata['name'];
$firstName=$userdata['given_name'];
$lastName=$userdata['family_name'];
$gplusURL=$userdata['link'];
$avatar=$userdata['picture'];
$gender=$userdata['gender'];
$dob=$userdata['birthday'];
//Execture query
$sql="insert into tbl_google(email,full_name,fname,lname,google_id,gender,dob,profile_image,gpluslink) values('$email','$fullName','$firstName','$lastName','$googleid','$gender','$dob','$avatar','$gplusURL')";
//$result=mysql_query($sql);
if($conn->query($sql)===TRUE)
{
die("error in storing data". $conn->error);
}
else
{
echo "record inserted successfully";
}
}
$conn->close();
}
?>