Tracking pixel implementation in html and php

Tracking pixel is usually used for tracking website visting stats. It is a 1 by 1 invisible image in an img tag that is usually placed in the header or the footer of a html page. The src for this img tag is usually an url link that generates the 1 by 1 pixel image. Some post parameters can be added to the url. When the html page is loaded, this pixel image is also loaded by requesting the image from the url in specified in the src of the img tag. The http request of this url will trigger the server side code, and the server side code gets notified that this pixel is triggered and it can get the post data from the requesting url.

Here is an example of a html page and server side php code to demonstrate the basic idea of tracing pixel.

index.html, nothing on this page, but a h1 tag and a img tag. The img tag is calling url trackingpixel.php?a=aaa&b=bbb to get an image. As you can see, it aslo posts two values, a=aaa and b=bbb.

<!DOCTYPE html>
		<title>Tracking Pixel</title>
		<h1>Tracking Pixel</h1>
		<img src="trackingpixel.php?a=aaa&b=bbb">

trackingpixel.php, the server side php code. When ever this is triggered, it will log the remote ip address that is trggering this code, and the post values of a, b, and c if any. Then it outputs the image content, and it will returned back the html page for the img tag.

//logger, make sure the directory or the file pixel.log is writable
function printLog($str)
  file_put_contents( 'pixel.log', $str."\n", FILE_APPEND | LOCK_EX );

//log some info
printLog(date('Y-m-d H:i:s'));
printLog('Remote Address: '.$_SERVER['REMOTE_ADDR']);
if (isset($_GET['a'])) {
	printLog('a: '.$_GET['a']);
if (isset($_GET['b'])) {
  printLog('b: '.$_GET['b']);
if (isset($_GET['c'])) {
  printLog('c: '.$_GET['c']);

//output the image
header('Content-Type: image/gif');

// This echo is equivalent to read an image, readfile('pixel.gif')
echo "\x47\x49\x46\x38\x37\x61\x1\x0\x1\x0\x80\x0\x0\xfc\x6a\x6c\x0\x0\x0\x2c\x0\x0\x0\x0\x1\x0\x1\x0\x0\x2\x2\x44\x1\x0\x3b";

Search within Codexpedia

Custom Search

Search the entire web

Custom Search