Google API v3 PHP会话丢失了他们的数据

时间:2012-10-02 16:27:37

标签: php cookies google-api session-variables

我正在构建一个使用Google API PHP客户端库的Web应用程序。该应用程序允许用户从Google Analytic Accounts中检索分析数据。除了我将要解释的以下内容之外,一切都有效。

第1步

用户在表单中选择一个帐户名,然后从accounts.php页面提交表单。然后,该帐户的帐户ID将保存在SESSION变量中。完成此操作后,accounts.php页面将使用位置标题重定向到simple.php页面。

第2步

simple.php页面显示一个Connect me链接,然后用户按此链接将其登录到Google,以便从分析中检索数据。此数据检索所有帐户。这些帐户然后针对帐户ID会话变量运行循环(以便检索用户在accounts.php页面中选择的正确帐户的结果)。

问题

问题在于,当Google重定向回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>";
}
?>

1 个答案:

答案 0 :(得分:2)

检查您提供的重定向URI是否在会话cookie的范围内。

例如,您指定'http://www.xxxx.co.uk/simple.php',但如果您最初通过'http://xxxx.co.uk/simple.php'(无www.)访问了该网页,则该Cookie可能超出范围。

这不仅适用于会话,也适用于一般的Cookie - 您可能会发现this值得一读。您可以使用函数session_set_cookie_params()控制PHP会话cookie的各种属性。可以将域设置为.xxxx.co.uk(前导.),以便它可以在根域及其所有子域中使用。