我正在将值保存到$_SESSiON
变量中,但在我提交表单后,我将其重定向到同一页面,$_SESSION
变量为空。
include 'googleauthorize.php';
include 'googleTransfer.php';
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_REQUEST['logout']))
unset($_SESSION['auth']);
if(isset($_REQUEST['auth'])){
//Initialize google client
$_SESSION['auth'] = new GoogleAuthorize(__DIR__ . '/../oauth-credentials.json', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']."?auth", "https://www.googleapis.com/auth/drive");
//Get authorize URL
$authUrl = $_SESSION['auth']->obtainAuthorizeUrl();
//Redirect
if(!isset($_GET['code'])){
$_SESSION['auth']->redirect($authUrl);
}
//Authentificate and return token
$_SESSION['token'] = $_SESSION['auth']->obtainAccessToken();
if(!isset($_SESSION['refreshToken']))
{
$_SESSION['refreshToken'] = $_SESSION['auth']->getRefreshToken();
//Call method to save token into DB
}
}elseif(isset($_REQUEST['upload'])){
if(isset($_SESSION['auth']))
{
//Initialize google client
$_SESSION['auth'] = new GoogleAuthorize(__DIR__ . '/../oauth-credentials.json', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], "https://www.googleapis.com/auth/drive");
}
//Here is undefined index 'token'
$_SESSION['auth']->setAccessToken($_SESSION['token']);
//Initialize new object for file transfer
new googleTransfer($_SESSION['auth']->getGoogleClient());
}
这是$_SESSION
语句中elseif
变量的内容。这让我感到困惑,因为里面只保存了auth
,里面没有设置任何东西。
Array
(
[auth] => GoogleAuthorize Object
(
[client:GoogleAuthorize:private] => Google_Client Object
(
[auth:Google_Client:private] => Google\Auth\OAuth2 Object
(
[authorizationUri:Google\Auth\OAuth2:private] => GuzzleHttp\Psr7\Uri Object
(
[scheme:GuzzleHttp\Psr7\Uri:private] => https
[userInfo:GuzzleHttp\Psr7\Uri:private] =>
[host:GuzzleHttp\Psr7\Uri:private] => accounts.google.com
[port:GuzzleHttp\Psr7\Uri:private] =>
[path:GuzzleHttp\Psr7\Uri:private] => /o/oauth2/auth
[query:GuzzleHttp\Psr7\Uri:private] =>
[fragment:GuzzleHttp\Psr7\Uri:private] =>
)
[tokenCredentialUri:Google\Auth\OAuth2:private] => GuzzleHttp\Psr7\Uri Object
(
[scheme:GuzzleHttp\Psr7\Uri:private] => https
[userInfo:GuzzleHttp\Psr7\Uri:private] =>
[host:GuzzleHttp\Psr7\Uri:private] => www.googleapis.com
[port:GuzzleHttp\Psr7\Uri:private] =>
[path:GuzzleHttp\Psr7\Uri:private] => /oauth2/v4/token
[query:GuzzleHttp\Psr7\Uri:private] =>
[fragment:GuzzleHttp\Psr7\Uri:private] =>
)
[redirectUri:Google\Auth\OAuth2:private] => http://localhost:81/FileTransfer/src/testik.php?auth
[clientId:Google\Auth\OAuth2:private] => 108715515230-g8e7tjh2d7luiggtfes6fsdv17n794hu.apps.googleusercontent.com
[clientSecret:Google\Auth\OAuth2:private] => Ab-DiaYYRnVSFf8JGay-TIMN
[username:Google\Auth\OAuth2:private] =>
[password:Google\Auth\OAuth2:private] =>
[scope:Google\Auth\OAuth2:private] =>
[state:Google\Auth\OAuth2:private] =>
[code:Google\Auth\OAuth2:private] =>
[issuer:Google\Auth\OAuth2:private] => 108715515230-g8e7tjh2d7luiggtfes6fsdv17n794hu.apps.googleusercontent.com
[audience:Google\Auth\OAuth2:private] =>
[sub:Google\Auth\OAuth2:private] =>
[expiry:Google\Auth\OAuth2:private] => 3600
[signingKey:Google\Auth\OAuth2:private] =>
[signingAlgorithm:Google\Auth\OAuth2:private] =>
[refreshToken:Google\Auth\OAuth2:private] =>
[accessToken:Google\Auth\OAuth2:private] =>
[idToken:Google\Auth\OAuth2:private] =>
[expiresIn:Google\Auth\OAuth2:private] =>
[expiresAt:Google\Auth\OAuth2:private] =>
[issuedAt:Google\Auth\OAuth2:private] =>
[grantType:Google\Auth\OAuth2:private] =>
[extensionParams:Google\Auth\OAuth2:private] => Array
(
)
)
[http:Google_Client:private] =>
[cache:Google_Client:private] =>
[token:Google_Client:private] =>
[config:Google_Client:private] => Array
(
[application_name] =>
[base_path] => https://www.googleapis.com
[client_id] => 108715515230-g8e7tjh2d7luiggtfes6fsdv17n794hu.apps.googleusercontent.com
[client_secret] => Ab-DiaYYRnVSFf8JGay-TIMN
[redirect_uri] => http://localhost:81/FileTransfer/src/testik.php?auth
[state] =>
[developer_key] =>
[use_application_default_credentials] =>
[signing_key] =>
[signing_algorithm] =>
[subject] =>
[hd] =>
[prompt] =>
[openid.realm] =>
[include_granted_scopes] =>
[login_hint] =>
[request_visible_actions] =>
[access_type] => offline
[approval_prompt] => auto
[retry] => Array
(
)
[cache_config] => Array
(
)
[token_callback] =>
)
[logger:Google_Client:private] =>
[deferExecution:Google_Client:private] =>
[requestedScopes:protected] => Array
(
[0] => https://www.googleapis.com/auth/drive
)
)
[authUrl:GoogleAuthorize:private] =>
https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=108715515230-g8e7tjh2d7luiggtfes6fsdv17n794hu.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A81%2FFileTransfer%2Fsrc%2Ftestik.php%3Fauth&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&approval_prompt=auto
[token:GoogleAuthorize:private] =>
)
)
答案 0 :(得分:2)
include 'googleauthorize.php';
include 'googleTransfer.php';
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_REQUEST['logout']))
unset($_SESSION['auth']);
if(isset($_REQUEST['auth']) || isset($_REQUEST['upload'])){
if(!isset($_SESSION['auth']){
$_SESSION['auth'] = new GoogleAuthorize(__DIR__ . '/../oauth-credentials.json', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']."?auth", "https://www.googleapis.com/auth/drive");
//Get authorize URL
$authUrl = $_SESSION['auth']->obtainAuthorizeUrl();
//Redirect
if(!isset($_GET['code'])){
$_SESSION['auth']->redirect($authUrl);
}
//Authentificate and return token
$_SESSION['token'] = $_SESSION['auth']->obtainAccessToken();
if(!isset($_SESSION['refreshToken']))
{
$_SESSION['refreshToken'] = $_SESSION['auth']->getRefreshToken();
//Call method to save token into DB
}
}
if(isset($_REQUEST['upload'])){
new googleTransfer($_SESSION['auth']->getGoogleClient());
}
}
答案 1 :(得分:2)
include 'googleauthorize.php';
include 'googleTransfer.php';
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_REQUEST['logout']))
unset($_SESSION['auth']);
if(isset($_REQUEST['auth'])){
// You have initialize the session variable auth here.
$_SESSION['auth'] = new GoogleAuthorize(__DIR__ . '/../oauth-credentials.json', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']."?auth", "https://www.googleapis.com/auth/drive");
//Get authorize URL
$authUrl = $_SESSION['auth']->obtainAuthorizeUrl();
//Redirect
if(!isset($_GET['code'])){
$_SESSION['auth']->redirect($authUrl);
}
$_SESSION['token'] = $_SESSION['auth']->obtainAccessToken();
if(!isset($_SESSION['refreshToken']))
{
$_SESSION['refreshToken'] = $_SESSION['auth']->getRefreshToken();
//Call method to save token into DB
}
}elseif(isset($_REQUEST['upload'])){
// but how will it set here? when not having value? You have put the //session initialize here also.
$_SESSION['auth'] = new GoogleAuthorize(__DIR__ . '/../oauth-credentials.json', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], "https://www.googleapis.com/auth/drive");
if(isset($_SESSION['auth']))
{
$_SESSION['auth']->setAccessToken($_SESSION['token']);
}
//Initialize new object for file transfer
new googleTransfer($_SESSION['auth']->getGoogleClient());
}