Reputation: 193
Before anything else is said, yes, I do have session_start();
on the top of every relevant page. That is not the issue.
I am working with Google Analytics API, and had a page where you could query Analytic data. I am using multiple drop-down menus to allow the user to choose what account they would like to query.
To populate these drop-down menus, I am using AJAX to call an API call to populate each drop-down menu depending what the previous drop-down's value was (think country->state->city).
Anyways, everything was working perfectly until I had to update the API library. Now I am losing my $_SESSION variables between my script that issues the AJAX POST request, and the script that processes the AJAX.
Here's my code:
index.php (the three drop-down menus, just note how they are instantiated)
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 -->
When a selection is made on the first drop-down menu, an AJAX request is made using this function:
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>
And this is the script that is not receiving any of the $_SESSION variables:
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>';
}
?>
I am receiving null
from $_SESSION['client']
in propertyID.php (and yes, I did save it into a session variable). In fact, when I refresh index.php, the cookies that allow me to access the API is cleared as well. Something is clearing the session somewhere, but I'm not sure how to approach this.
Any suggestions would be greatly appreciated!
Upvotes: 1
Views: 609
Reputation: 480
There's a workaround you can call
session_start($session-id);
and start a session whenever you want.
You can get session id by reading cookies in $_COOKIES
variable thats it.
Upvotes: 0
Reputation: 193
Figured out my issue...
For whatever reason, the library I upgraded to was giving me issues and wiping my session. It makes zero sense, but getting a different version resolved the issue.
Thanks to all who put in the effort to read my problem.
Upvotes: 1