Php 重定向后保存值
我正在将值保存到Php 重定向后保存值,php,Php,我正在将值保存到$\u SESSiON变量中,但在提交表单(将我重定向到同一页面)后,$\u SESSiON变量为空。 include 'googleauthorize.php'; include 'googleTransfer.php'; if (session_status() == PHP_SESSION_NONE) { session_start(); } if (isset($_REQUEST['logout'])) unset($_SESSION['auth'])
$\u SESSiON
变量中,但在提交表单(将我重定向到同一页面)后,$\u 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());
}
这是elseif
语句中的$\u会话
变量的内容。这让我很困惑,因为里面只保存了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] =>
)
)
session\u start()应始终被调用,因此请尝试删除没有帮助的if(session\u status()==PHP\u session\u NONE),我仍然会得到未定义的索引。未定义的索引用于哪个变量?对于变量
$\u session['token']
,我正试图在elseif
blockEDITED中访问它:您设置了$\u session['token']
在if语句中,但是如果转到elseif
块会发生什么?应该始终调用session\u start(),因此尝试删除没有帮助的if(session\u status()==PHP\u session\u NONE),我仍然会得到未定义的索引。未定义的索引针对哪个变量?对于变量$\u session['token']
我正试图在elseif
blockEDITED中访问它:您在if语句中设置了$\u SESSION['token']
,但是如果您转到elseif
块会发生什么?是的,这会起作用,但我还需要使用重定向
以及第一个if
语句中的几乎所有内容。基本上,我将再次进行身份验证,这是我不想要的,因为我已经在进行身份验证了,我只需要以某种方式获取令牌
,我想将其保存到$\u SEASSON['token']
到elseif
块中,然后尝试不使用和if/else。放置一条if语句,该语句创建身份验证并刷新令牌。之后,放入另一个if语句来检查上传。这样,您将始终创建一个身份验证和一个令牌。我不这样做吗elseif
和我有第二个if
语句时一样。如果我误解了你,你能重新安排你的密码吗?@jemcaj编辑了答案。你能试试吗?@jemcaj我不知道你到底需要什么,因为我不知道你到底想要实现什么。但是,您应该确保在尝试上载之前设置了auth
和token
。是的,这会起作用,但我还需要使用redirect
和第一个if
语句中的几乎所有内容。基本上,我将再次进行身份验证,这是我不想要的,因为我已经在进行身份验证了,我只需要以某种方式获取令牌
,我想将其保存到$\u SEASSON['token']
到elseif
块中,然后尝试不使用和if/else。放置一条if语句,该语句创建身份验证并刷新令牌。之后,放入另一个if语句来检查上传。这样,您将始终创建一个身份验证和一个令牌。我不这样做吗elseif
和我有第二个if
语句时一样。如果我误解了你,你能重新安排你的密码吗?@jemcaj编辑了答案。你能试试吗?@jemcaj我不知道你到底需要什么,因为我不知道你到底想要实现什么。但是,在尝试上载之前,您应该确定<代码> Auth<代码>和代码>令牌< /C>。请考虑编写一个正确的答案,而不是仅仅编辑代码并放置简短的注释。如果你能在lil浓度下看到这一点理解它不需要一点点的专注。这是清楚的,解释得很好。感谢ALOTS:)请考虑写一个正确的答案,而不是只是编辑代码和简短的评论。如果你能在lil浓度下看到这一点理解它不需要一点点的专注。这是清楚的,解释得很好。非常感谢:)
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());
}
}
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());
}