Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.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的不同页面中获取会话数据_Php_Session - Fatal编程技术网

无法在php的不同页面中获取会话数据

无法在php的不同页面中获取会话数据,php,session,Php,Session,我不知道为什么会话数据不能在不同的页面中访问。在下面的代码中,我创建了一个名为“google_数据”的会话来存储google用户的数据。在denied.php中尝试访问该会话时,会话数据不可用。有人帮我吗 googleauthn.php **<?php //require_once 'src/apiClient.php'; //require_once 'src/contrib/apiOauth2Service.php'; session_start(); require_once di

我不知道为什么会话数据不能在不同的页面中访问。在下面的代码中,我创建了一个名为“google_数据”的会话来存储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>";
}
?>**
<?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();

        }

?>
****
denied.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>";
}
?>**
<?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();

        }

?>

如果($client->getAccessToken())
工作正常,您确定
吗?您需要在php标记之前删除*。如果在Session_start()之前显示文本,则不会设置Session。我已删除了文本,现在Session_start()位于denied.php的顶部。但不会显示会话数据。现在,如果(!isset($_SESSION['google_data']){//重定向到应用程序主页//头(“location:index.php”);echo'bad SESSION';}bock被执行。