Using PHP generated Images for Website Statistics and Analytics

Article Index
Using PHP generated Images for Website Statistics and Analytics
Adding Statistics Recording
Passing Parametes via the URL to your Image
Optimizing your Final PHP Code
Further Discussion

Though used mainly for generating HTML, you can also generate any other file such as an Image with PHP. Normally you would use special libraries such as Imagemagik or GD to assist PHP in generating Images, however, for this example we do no manipulate any image data, so we will not be needing any special PHP libraries.

The first thing to understand when you generate Images (or any file) using PHP is how a User-Agent, such as a browser, identifies file types. By default a browser assumes that any file is an HTML file unless specified otherwise. The file type is specified in the HTTP Response that your PHP Server sends to the browser, and not the file extension, as commonly misunderstood. A Web Server specifies the file type using the HTTP Content-Type Header, is defined in section 14 of the HTTP Specifications.

A HTTP Response consists of two main parts, the HTTP Response Headers and the HTTP Body. The Content-Type HTTP Response Header is what defines the type of file that is being transmitted over HTTP.

An example HTTP Response Headers for an Image is:

HTTP/1.1 200 OK
Date: Fri, 02 Nov 2007 03:44:13 GMT
Server: Apache/2.0
Content-Type: image/jpg
Content-Length: 123

The Response Header of interest is: Content-Type: image/jpg. This tells the browser that this is an image, instead of HTML.

PHP offers the ability to modify the HTTP Response Headers that are transmitted with the output of your PHP script through the header() function. We can use this to send the Image headers.

<?php
header('Content-Type: image/png');
?>

The easiest way to output the contents of an Image is to read the contents from an existing image and echo it from your php script. So you will need to create an image and place it on your server (or you can use any image on the web if your PHP server allows outgoing socket connections - set in PHP.ini as allow_url_fopen = true).

For this example, we will use the google logo. But you can use any image. To get the Image into your PHP memory, we use:

<?php
header('Content-Type: image/png');
$img_data = file_get_contents('http://www.google.com.fj/intl/en_com/images/logo_plain.png');
?>

Now the data we receive is binary image data. But we don't need to worry about what type of data this is, we are just buffering it into our variable $img_data, so we can send it back out as the output of PHP script.

Now all we have to do is echo this image data back out.

<?php
header('Content-Type: image/png');
$img_data = file_get_contents('http://www.google.com.fj/intl/en_com/images/logo_plain.png');
echo $img_data;
?>

Now we have a snippet of code that will dump an image into the browser.

Now we save the PHP code to a file, so we can include it in our HTML. So if you save your PHP file as http://example.com/tracker.php, then you can include it HTML on any webpage as:

<img src="http://example.com/tracker.php" />

Every time this image is requested from your server, it is actually requesting your PHP script.