Getting the IPs from AWS on a load balancer using Piwik Stats

Posted on Updated on

I’ll keep this short and give the general gist of this.

Basically I had a problem, whereby Piwik was not getting the correct IP of the visitor, as the AWS setup was not passing the correct IP through on the expected value.

Try this code out as a test and see what you get.

Getting server/client IP:<br>

<?php
var_dump($_SERVER['SERVER_ADDR']);
var_dump($_SERVER['SERVER']); 
var_dump($_SERVER['HTTP_X_FORWARDED_PORT']);
var_dump($_SERVER['HTTP_X_FORWARDED_PROTO']);
var_dump($_SERVER['HTTP_X_FORWARDED_FOR']);
var_dump($_SERVER['SERVER_ADDR']);
var_dump($_SERVER['SERVER_PORT']);
var_dump($_SERVER['REMOTE_ADDR']);

echo "1: " . $_SERVER['SERVER_ADDR'] . "<br>";
echo "2: " . $_SERVER['SERVER'] . "<br>";
echo "3: " . $_SERVER['HTTP_X_FORWARDED_PORT'] . "<br>";
echo "4: " . $_SERVER['HTTP_X_FORWARDED_PROTO'] . "<br>";
echo "5: " . $_SERVER['HTTP_X_FORWARDED_FOR'] . "<br>";
echo "6: " . $_SERVER['SERVER_ADDR'] . "<br>";
echo "7: " . $_SERVER['SERVER_PORT'] . "<br>";
echo "8: " . $_SERVER['REMOTE_ADDR'] . "<br>";

echo phpinfo();
?>

As an intro, to help you figure out the correct value, use the above code as a test.

The problem with the suggestions on the Piwik site saying to make changes to these files, is that is didn’t work for me.

[General]
; Uncomment line below if you use a standard proxy
;proxy_client_headers[] = HTTP_X_FORWARDED_FOR
;proxy_host_headers[] = HTTP_X_FORWARDED_HOST

; Uncomment line below if you use CloudFlare
;proxy_client_headers[] = HTTP_CF_CONNECTING_IP

; Uncomment line below if you use another proxy (that sets Client-IP header)
;proxy_client_headers[] = HTTP_CLIENT_IP

You have more luck, but it’s for viewing here: https://piwik.org/faq/how-to-install/faq_98/

So, while there are many values in Piwik to have various settings, it’s worth filling in as many as possible…

The key value for this IP issue is this:

$IP_Tracker = $_SERVER['HTTP_X_FORWARDED_FOR'];

Here’s an example that will probably work for you. It’s pulled from a live site, but modified for anonymous use).

<?php

global $IDSITE;
$IDSITE = 2;  
    
// This is so we can get the correct IP of the visitor from behind the AWS load balancer.    
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) 
    { $IP_Tracker = $_SERVER['HTTP_X_FORWARDED_FOR'];} else
    { $IP_Tracker = $_SERVER['REMOTE_ADDR']; }
$pagetitle = "Home"; // The name of the tab for the stats.
// Tracking Code
// -- Piwik Tracking API init -- 
require_once "PiwikTracker.php";
PiwikTracker::$URL = 'http://loadbalancer.serverlocation.com/piwik/';

$t = new PiwikTracker( $IDSITE, 'http://loadbalancer.serverlocation.com/piwik/');

// End of configuration settings.  

// Optional function calls
$t->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$t->setBrowserLanguage('en');
$vid = '';

// Allow the follow-through tracking from page to page. - Visitor ID
if( isset($_POST['vid']) ) { $vid = addslashes($_POST['vid']); } else { if( isset($_GET['vid']) ) { $vid = addslashes($_GET['vid']); } }; // get vid
if(strlen($vid) > 10 && strlen($vid)< 30) { $t->setVisitorId($vid); }; // double check it
 
// If you want to force the visitor IP, or force the server date time to a date in the past,
// it is required to authenticate the Tracking request by calling setTokenAuth
// You can pass the Super User token_auth or any user with 'admin' privilege on the website $idSite

$token_auth = "YOURTOKEN";
$t->setTokenAuth( $token_auth );
$t->setIp( $IP_Tracker );
  
// If you wanted to force to record the page view or conversion to a specific visitorId
$t->setUrl($url = 'https://app.yoursever.com/2015_home_tab.php');

// Finally, track the page view with a Custom Page Title
// In the standard JS API, the content of the <title> tag would be set as the page title
$t->doTrackPageView($pagetitle);
?>

<a href="http://app.yoursitecom/page2.php?vid=<?php echo $t->getVisitorId(); ?>">Page 2</a>

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s