I am building a Web application that uses the Google API PHP client library. The application allows the user to retrieve analytical data from Google Analytic Accounts. Everything works how it should apart from the following of which I am going to explain.
Step 1
The user selects an account name in the form and submits the form from the accounts.php page. The account id for that account is then saved in a SESSION variable. Once this is done the accounts.php page redirects to the simple.php page using location header.
Step 2
The simple.php page displays a Connect me link which the user then presses to log them into Google in order to retrieve the data from analytics’s. This data retrieves all the accounts. These accounts then run through a loop against the account id Session variable (in order to retrieve results for the correct account that the user selected in the accounts.php page).
The problem
The problem is that when Google redirects back to the simple.php page all the session data that I stored before hand in the accounts.php is lost. Even if I store the account id in a cookie. I think the Google PHP client library is wiping my session data somehow in the backend. I have tried so many ideas and all of them dont seem to work. Strangely enough though if I log out, or go back to accounts.php and try again, the Session variable holds its account ID data after the Google redirect. So confused!
I hope I have made that as clear as possible
<?php
session_start();
/*
if ($_SESSION['test'] != "logged" || $_SESSION['test'] == null) {
header('Location: http://www.xxxx.co.uk/');
die("Redirecting to home page");
}*/
header("Accept-Encoding: gzip");
header("User-Agent: gzip");
require_once 'src/apiClient.php';
require_once 'src/contrib/apiAnalyticsService.php';
$client = new apiClient();
$client -> setApplicationName("Google Analytics PHP Starter Application");
$client -> setClientId('xxxx');
$client -> setClientSecret('xxxx');
$client -> setRedirectUri('http://www.xxxx.co.uk/simple.php');
$client -> setDeveloperKey('xxxx');
$client -> setUseObjects(true);
$service = new apiAnalyticsService($client);
$table = array();
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client -> authenticate();
$_SESSION['token'] = $client -> getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client -> setAccessToken($_SESSION['token']);
}
if ($client -> getAccessToken()) {
$accounts = $service -> management_accounts -> listManagementAccounts(array("fields" => "items(id,name)"));
print "<a class='login' href='index.php?logout=true'>Logout</a>";
//$accounts = $service -> management_accounts -> listManagementAccounts();
foreach ($accounts->getItems() as $account) {
//var_dump($_SESSION['account_number']);
if ($account -> getId() == $_COOKIE["test"]) {
echo "</br>" . $account -> getName() . "</br>";
$props = $service -> management_webproperties -> listManagementWebproperties($account -> getId());
//foreach ($props->getItems() as $property) {
$profiles = $service -> management_profiles -> listManagementProfiles($account -> getId(), "~all");
foreach ($profiles -> getItems() as $profile) {
$counter++;
$table[$counter]['name'] = $profile -> getName();
echo "</br>" . $profile -> getName() . "</br>";
/*
echo "<h1>Profiles</h1>";
echo "<pre>";
var_dump($profile);
echo "</pre>";*/
// Direct
$data = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-7"));
// Non Paid
$data2 = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-5"));
// Paid
$data3 = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-4"));
// Refferal
$data4 = $service -> data_ga -> get("ga:" . $profile -> getId(), $_SESSION['startdate'], $_SESSION['enddate'], 'ga:visits', array("segment" => "gaid::-8"));
/*
echo "<h1>Non Paid Data</h1>";
echo "<pre>";
//var_dump($data);
echo "<pre>";
echo "<h1>Paid Data</h1>";
echo "</pre>";
//var_dump($data2);
echo "<pre>";
echo "<h1>Search Data</h1>";
echo "<pre>";
//var_dump($data3);
echo "</pre>";
echo "<h1>Referral Data</h1>";
echo "<pre>";
//var_dump($data4);
echo "</pre>";*/
$results1 = $data -> getTotalsForAllResults();
$results2 = $data2 -> getTotalsForAllResults();
$results3 = $data3 -> getTotalsForAllResults();
$results4 = $data4 -> getTotalsForAllResults();
foreach ($results1 as $result) {
echo "Direct Traffic " . $result . "<br/>";
}
foreach ($results2 as $result) {
$table[$counter]['nonpaid'] = $result;
echo "Non Paid Traffic " . $result . "<br/>";
}
foreach ($results3 as $result) {
echo "Paid Traffic " . $result . "<br/>";
}
foreach ($results4 as $result) {
$table[$counter]['refferal'] = $result;
echo "Refferal Traffic " . $result . "<br/>";
}
}
} else {
echo "This is not working";
var_dump($_COOKIE["test"]);
}
$_SESSION['token'] = $client -> getAccessToken();
}
echo "<table class='data'>";
foreach($table as $data) {
$total = $data['nonpaid'] + $data['refferal'];
echo "<tr><td>".$data['name']."</td><td>".$total."</td></tr>";
}
echo "</table>";
} else {
$authUrl = $client -> createAuthUrl();
var_dump($_COOKIE["test"]);
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
Check that the redirect URI you have supplied is within the scope of your session cookie.
For example, you specify
'http://www.xxxx.co.uk/simple.php', but if you accessed the page originally via'http://xxxx.co.uk/simple.php'(nowww.) then the cookie may be out of scope.This does not only apply to sessions, but also to cookies in general – you might find this to be worth a read. You can control the various properties of the PHP session cookie using the function
session_set_cookie_params(). It is possible to set the domain to.xxxx.co.uk(leading.) so it will be available on the root domain and all its subdomains.