在说出任何其他内容之前,是的,我在每个相关页面的顶部都有session_start();
。这不是问题。
我正在使用Google AnalyticsAPI,并且有一个页面,您可以在其中查询分析数据。我使用多个下拉菜单允许用户选择他们想要查询的帐户。
要填充这些下拉菜单,我使用AJAX调用API调用来填充每个下拉菜单,具体取决于前一个下拉列表的值(想想country-> state-> city)。 / p>
无论如何,一切都运行良好,直到我不得不更新API库。现在我在发出AJAX POST请求的脚本和处理AJAX的脚本之间丢失了$ _SESSION变量。
这是我的代码:
index.php(三个下拉菜单,只需注意它们是如何实例化的)
if ($client->getAccessToken()) { //if the client has a valid access token
try {
//lists the client's available analytic accounts
$accounts = $analytics->management_accounts->listManagementAccounts();
$accsAvailable = $accounts->getItems(); ?>
<p>Available Accounts:</p> <?php
if (!isset($_REQUEST['compare'])) { ?>
<form action="" name="formSubmit" onsubmit="return validateForm()" method="post"> <!--START FORM--> <?php
} else { ?>
<form action="" name="formSubmit" onsubmit="return validateComparison()" method="post"> <!--START FORM--> <?php
} ?>
<select name="dropAccounts" class="dropAccounts" id="dropAccounts"> <!--drop-down for avaiable accounts--> <?php
//if there is at least one account available
if (count($accsAvailable) > 0) { //create a drop down of available accounts ?>
<option value="0">---Select an account---</option> <?php //default option
foreach ($accsAvailable as $account) {
//populate from API
echo '<option value=' . $account->getId(). '>' . $account->getName() . '</option>';
}
} else { ?>
<option value="0">---No accounts available---</option> <?php //else no accounts exist
} ?>
</select> <!--END drop-down for avaiable accounts-->
</div>
<!-- /.col-md-4 -->
<div class="col-md-4">
<p>Available Webproperties:</p> <!--drop-down for available webproperties-->
<select name="dropProperties" class="dropProperties" id="dropProperties">
<option selected="selected" value="0">---Select a webproperty---</option> <!--default option-->
</select> <!--END drop-down for available webproperties-->
</div>
<div class="col-md-4">
<p>Available Profiles: </p> <!--drop-down for available profiles-->
<select name="dropProfiles" class="dropProfiles" id="dropProfiles">
<option selected="selected" value="0">---Select a profile---</option> <!--default option-->
</select> <!--END drop-down for available profiles-->
</div>
</div>
<!-- /.row -->
在第一个下拉菜单中进行选择时,使用此功能发出AJAX请求:
Javascript AJAX:
<script type="text/javascript">
$(document).ready(function() {
//populates the properties drop-down from API call
function populateProperties(accountID) {
$.ajax
({
type: "POST",
url: "/scripts/propertyID.php",
data: {
'accountID' : accountID //sends account ID for processing
},
cache: false,
success: function(html) {
$("#dropProperties").html(html); //refresh the properties drop-down
populateProfiles($("#dropProperties > option:selected").val()); // Populate profiles after properties load
}
});
}
//populates the profiles drop-down from API call
function populateProfiles(propertyID) {
$.ajax
({
type: "POST",
url: "/scripts/profileID.php",
data: {
'propertyID' : propertyID //sends property ID for processing
},
cache: false,
success: function(html) {
$("#dropProfiles").html(html); //refresh the profiles drop-down
}
});
}
//onchange event - repopulates properties and profiles after changing account
$("#dropAccounts").change(function() {
var accountID = $("#dropAccounts > option:selected").val(); //gets the account ID from drop-down value
populateProperties(accountID); //repopulates properties drop-down
});
//onchange event - repopulates profiles after changing properties
$("#dropProperties").change(function(){
var propertyID = $("#dropProperties > option:selected").val(); //gets the profile ID from drop-down value
populateProfiles(propertyID); //repopulates the profiles drop-down
});
});
</script>
这是没有收到任何$ _SESSION变量的脚本:
propertyID.php
<?php
session_start();
//required Google Analytics libraries
set_include_path("../../lib/google-api-php-client-1.0.4-beta/src");
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
if($_POST['accountID'])
{
$account = $_POST['accountID'];
$_SESSION['account_id'] = $accountID;
$client = $_SESSION['client'];
if ($client != null) {
$analytics = new Google_Service_Analytics($client);
if ($accountID != "0") {
$webProperty = $analytics->management_webproperties->listManagementWebproperties($accountID);
$webItem = $webProperty->getItems();
foreach ($webItem as $item) {
echo '<option value=' . $item->getId() . '>' . $item->getName() . '</option>';
}
}
}
} else {
echo '<option value="0">---Select a webproperty---</option>';
}
?>
我在propertyID.php中从null
收到$_SESSION['client']
(是的,我确实将其保存到会话变量中)。实际上,当我刷新index.php时,允许我访问API的cookie也会被清除。有些事情正在某个地方清理会议,但我不确定如何处理这个问题。
任何建议都将不胜感激!
答案 0 :(得分:0)
想出我的问题......
无论出于何种原因,我升级到的图书馆都给了我问题并擦除了我的会话。这没有任何意义,但获得不同的版本解决了这个问题。
感谢所有努力阅读我的问题的人。
答案 1 :(得分:0)
您可以致电
session_start($session-id);
并随时启动会话。
您可以通过阅读$_COOKIES
变量中的Cookie来获取会话ID。