Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:42:07+00:00 2026-06-12T09:42:07+00:00

I am building a Web application that uses the Google API PHP client library.

  • 0

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>";
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:42:09+00:00Added an answer on June 12, 2026 at 9:42 am

    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' (no www.) 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building an web application for a customer that uses a rich client.
I am building a web application that allows the user to upload high res
I'm building a client-server (c#) application that uses a web services to synchronize the
I am building a web application that uses the UCMA API 3.0 to establish
We're building a blog-like Pylons web application that uses CKeditor and allows to upload
I built a web application that uses Yahoo Geocoding API or Google Maps API
I'm building a rich web application that uses a lot of data. When I'm
I am building a web application that is currently PHP with MySQL. There is
We're building a latency-sensitive web application that uses websockets (or a Flash fallback) for
I'm currently building a web application that has multiple user types, where users engage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.