We help IT Professionals succeed at work.

Tracking pixels

eezar21
eezar21 asked
on
Ok,  So I have a project for my own education and a practical application.  
I want to be able to drop a pixel onto my users on my websites (gamerss.co.uk, and whatsagirltodo.co.uk) and be able to assign a user an ID number and then be able to see or identify them when I come back.

I have Plesk set up on my VPS server and therefore a database ready to go.  I am ok with writing to the database and extracting information from it, however I don't know where to start to be able to generate a unique pixel that will be able to read a (unique?) IP address and allocate it a number in the database for each visit?

Ulitmately I want to be able to use the information to output into an excel sheet or even better be able to send that user a message when they return based upon their IP location.  BTW I have privacy policies ready - and I am not interested in finding out data about individuals.

I'm sure there are a ton of questions this is going to throw up, but please if anybody can help point me in the right direction or help me with a snippet of code, the I can start learning :)

Comment
Watch Question

Mmmmmm..... not very sure about what or why do you want to do with a "pixel".

Why don't you simply get the IP from the client computer or your logged user and save it on database? You even can save more than one ip for every user (depending on their logins) so you can detect when a user is logging in from a different computer than it's usual one and you'll be somewhat able to track a bit users that hasn't a fixed IP (which will usually be the most of them)

Author

Commented:
Thanks Bardobrave.

I don't have a login for my users as they are just blog sites.

I also have banner ads for the sites to promote them.  I want to also be able to drop a pixel with the banner so that I can collect data about where the banners are, rather than having google or an ad network holding my data when someone clicks onto the banner.  If I just collect the data by using a hosted solution on the site then I will only get part of what I want.  Ultimately I want to be able to target my ads to the IP addresses of my users - but I need some way of calling that data back into a URL referred pixel.

I hope that all makes sense - I'm sure you will tell me if it doesn't
Most Valuable Expert 2011
Top Expert 2016

Commented:
be able to drop a pixel

The thing you want is an HTTP cookie.  In PHP, you use it via setcookie().
Most Valuable Expert 2011
Top Expert 2016
Commented:
In the code snippet there is a little script you can install and run.  It will show you something of how cookies work.  This article may be helpful, too.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3314-How-to-Show-an-Introductory-Web-Page-Once-Using-PHP.html

You can almost always find the client IP address in the variable at $_SERVER["REMOTE_ADDR"].  You would capture that value, store it in a data base along with any other information you wanted to keep about the client (pages visited, etc.).  Then you would place a long-term cookie on the browser with the key to the data base row.  With each succeeding visit you can access the data base to see the client's history.

HTH, ~Ray
<?php // RAY_cookie_example.php
error_reporting(E_ALL);


// RECEIVE FORM INPUT AND SET A COOKIE WITH THE NAME, VALUE AND LIFE FROM THE FORM
// MAN PAGE: http://php.net/manual/en/function.setcookie.php
// TO SEE COOKIES IN FIREFOX, FOLLOW SOMETHING LIKE TOOLS => OPTIONS => PRIVACY => SHOW COOKIES (OR "REMOVE INDIVIDUAL")


// REQUIRED AT PHP 5+
date_default_timezone_set('America/New_York');


// IF THE FORM HAS BEEN POSTED
if (!empty($_POST))
{
    // TIDY UP THE POST INPUT - CLEAN AND NOT MORE THAN 16 BYTES
    $name = substr(clean_string($_POST["name"]),0,16);
    $data = substr(clean_string($_POST["data"]),0,16);
    $life = clean_number($_POST["life"]);

    // BE SURE WE HAVE USEFUL INFORMATION
    if ( ($name == '') || ($data == '') ) die("MISSING INPUT: PLEASE <a href=\"{$_SERVER['PHP_SELF']}\">TRY AGAIN</a>");



    // CHOOSE THE COOKIE NAME, VALUE, AND LIFE IN SECONDS
    $cookie_name    = $name;
    $cookie_value   = $data;
    $cookie_life    = $life;



    // CONFIGURE THE COOKIE LIFE
    if ($life == 0)
    {
        // USE THIS TO MAKE COOKIE EXPIRE AT END OF BROWSER LIFE
        $cookie_expires = 0;
    }
    else
    {
        // USE THIS TO MAKE A PERSISTENT COOKIE
        $cookie_expires = time() + $cookie_life;
    }



    // MAKE THE COOKIE AVAILABLE TO ALL DIRECTORY PATHS
    $cookie_path	= '/';



    // MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS).
    $x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
    $y = count($x);
    if ($y == 1) // MAYBE 'localhost'?
    {
        $cookie_domain = $x[0];
    }
    else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
    {
        // USE THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN
        $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1];
    }



    // MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS
    $cookie_secure    = FALSE;



    // HIDE COOKIE FROM JAVASCRIPT (PHP 5.2+)
    $cookie_http      = TRUE;



    // SET THE COOKIE
    if ( setcookie
         ( $cookie_name
         , $cookie_value
         , $cookie_expires
         , $cookie_path
         , $cookie_domain
         , $cookie_secure
         , $cookie_http
         )
       )
    {
        echo PHP_EOL . "<br/>SUCCESS!  THE COOKIE HAS BEEN SET AND WILL BE AVAILABLE TO THE NEXT PAGE LOAD";
    }
    else
    {
        echo PHP_EOL . "<br/>FAILURE!  THE COOKIE WAS NOT SET AS EXPECTED";
    }



    // AT THIS POINT, THE COOKIE HAS BEEN SET, BUT IT IS NOT AVAILABLE IN THIS SCRIPT
    // THE COOKIE WILL NOT BE RETURNED FROM THE CLIENT TO THE SERVER UNTIL THE NEXT HTTP REQUEST
    // THIS IS BECAUSE THE BROWSER SENDS THE COOKIE TO OUR SCRIPT BEFORE OUR SCRIPT STARTS RUNNING
    echo '<pre>';
    echo PHP_EOL . '$_COOKIE CONTAINS '; var_dump($_COOKIE);
    echo PHP_EOL . '$_POST CONTAINS ';   var_dump($_POST);
    echo PHP_EOL . 'THE COOKIE HAS BEEN SET WITH THESE VALUES: ';
    echo PHP_EOL . 'COOKIE NAME:    ' . $cookie_name;
    echo PHP_EOL . 'COOKIE VALUE:   ' . $cookie_value;
    echo PHP_EOL . 'COOKIE EXPIRES: ' . number_format($cookie_expires) . " == " . date('r', $cookie_expires);
    echo PHP_EOL . 'COOKIE PATH:    ' . $cookie_path;
    echo PHP_EOL . 'COOKIE DOMAIN:  ' . $cookie_domain;
    echo PHP_EOL . 'COOKIE SECURE:  '; var_dump($cookie_secure);
    echo           'COOKIE HTTP:    '; var_dump($cookie_http);
    echo "</pre>";
}


// END OF SETTING THE COOKIE - CREATE THE FORM IN HEREDOC NOTATION
$self = $_SERVER["PHP_SELF"];
$form = <<<FORM
<pre>
<form method="post">
COOKIE NAME: <input name="name" /> STRING
COOKIE DATA: <input name="data" /> STRING
COOKIE LIFE: <input name="life" /> INTEGER SECONDS
<input type="submit" />
<b>TO SEE THE EXISTING COOKIES <a href="$self">CLICK HERE</a></b>
</form>
</pre>
FORM;
echo $form;



// SHOW THE COOKIE ARRAY, IF ANY
echo '<pre>$_COOKIE CONTAINS '; var_dump($_COOKIE); echo "</pre>";



// UNRELATED FUNCTIONS TO TIDY UP FORM INPUT
function clean_string($string)
{
    return trim(preg_replace('/[^A-Z0-9_]/i', NULL, $string));
}

function clean_number($string)
{
    return trim(preg_replace('/[^0-9]/i',     0,    $string));
}

// ACTIVATE THIS TO SHOW THE SCRIPT CODE
// die( highlight_file(__FILE__, TRUE) );

Open in new window

Ok, I think now I'm understanding what you want, maybe this article could help you:

http://www.ehow.com/how_5277834_use-pixel-tracking.html

Ask if you have further questions or doubts.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I think the pixel tracker may not be able to help you with the IP address.  You will need to capture that separately in your PHP script.  But once you've got the IP address, all kinds of opportunities open up for you.  See the page here, for an example:
http://landonbaseball.com/site_visitors.php

Each of those pushpins identifies an IP-to-location match.  Given the known locations, you can reverse-geocode the ZIP code.  Once you know the ZIP code, you can get demographic information about housing, income distributions, etc, free of charge from the US census.  It's good business intelligence.  

See http://www.laprbass.com/RAY_what_is_my_ip_address.php
<?php // RAY_what_is_my_ip_address.php
error_reporting(E_ALL);


$str
= "<h1>"
. $_SERVER["REMOTE_ADDR"]
. "</h1>"
. PHP_EOL;

echo $str;

Open in new window

Author

Commented:
Thanks very much - this has been a great help for me - I have managed to capture most of the information that I need.  Still need to play around with setting the cookie, but this is just what I needed to get me on my way - thanks