我正在構建一個使用Google API PHP客戶端庫的Web應用程序。該應用程序允許用戶從Google分析帳戶中檢索分析數據。除了我將要解釋的以下內容之外,所有內容都應該如何運作。Google API v3 PHP會話丟失了他們的數據
步驟1
用戶在形式選擇的帳戶名稱,然後從accounts.php頁面提交表單。該帳戶的帳戶ID然後保存在SESSION變量中。完成此操作後,accounts.php頁面將使用位置標題重定向到simple.php頁面。
步驟2
的simple.php頁面顯示連接我鏈接,然後用戶按下他們登錄到谷歌爲了檢索分析的數據。該數據檢索所有帳戶。然後,這些帳戶將通過一個針對帳戶ID Session變量的循環運行(以便檢索用戶在accounts.php頁面中選擇的正確帳戶的結果)。
問題
的問題是,當谷歌重定向回simple.php頁面,我的手之前存儲在accounts.php將丟失所有的會話數據。即使我將帳戶ID存儲在cookie中。我認爲Google PHP客戶端庫在後端以某種方式清除了我的會話數據。我已經嘗試了很多想法,並且他們似乎都不工作。奇怪的是,如果我退出帳戶,或返回accounts.php並再次嘗試,則Session變量在Google重定向後保存其帳戶ID數據。如此迷茫!
我希望我已經作出儘可能
<?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>";
}
?>
檢查您提供的重定向URI是否在會話cookie的範圍內。例如,你指定''http:// www.xxxx.co.uk/simple.php',但是如果你最初通過http:// xxxx.co.uk/simple.php'來訪問頁面(不是'''),那麼cookie可能超出範圍。 – DaveRandom
@DaveRandom我明天不會回來工作,所以我無法檢查,但如果您有權利,我可能不得不追捕您,並給您一個大吻。只是做了一些小測試我的其他網站,它似乎很有前途。 –
是否有關於此問題的任何文檔。我從來不知道www。受影響的會話 –