How to make a simple tracking image with PHP

By • Nov 24th, 2008 • Category: Advice

I have been doing a lot of tracking recently, whether it be in e-mails, on social networks, web sites, or even on message boards, and I figured I might as well share one of the methods I use to track visitors using as little code as possible.

This tutorial will teach you how to track visitors using simple PHP. If combined with JavaScript as done with Open Analytics or an e-commerce platform as done on Drop Cart, you can track a whole lot more.

Step 1: Figure out what you want to track

[ ] IP Address
[ ] City & State
[ ] Web Browser
[ ] Operating System
[ ] JavaScript Capabilities
[ ] Cookie Capabilities
[ ] Page Visited
[ ] …

Step 2: Figure out what your server can handle

[ ] GD/GD2
[ ] MySql
[ ] SQLite
[ ] Browscap

If you have GD installed:

$im=imagecreatetruecolor(1,1);imagecolortransparent($im,imagecolorallocate($im,0,0,0));header(‘Content-type: image/png’);imagepng($im);imagedestroy($im);


header(‘Content-type: image/png’);echo gzinflate(base64_decode(‘6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfs

Step 3: Do the tracking

This part’s up to you. Decide how you want to save or send the data you track (MySQL,SQLite,E-Mail,Text Message) and do it.

Here’s an example of how you might track e-mail receipts using a hidden image:

This method uses SQLite3 and PDO so they will have to be enabled in php.ini

The .htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^([A-z0-9]{6}).png$ /tracker.php?id=$1 [L]

The database:

CREATE TABLE “hits” (“id” TEXT, “ip” TEXT, “page” TEXT, “timestamp” INTEGER)

The PHP file:

header(‘Content-type: image/png’);echo gzinflate(base64_decode(‘6wzwc+flkuJiYGDg9fRwCQLSjCDMwQQkJ5QH3wNSbCVBfs
$db=new PDO(‘sqlite:’.dirname(dirname(__FILE__)).’/openanalytics.sqlite’);
$db->prepare(“INSERT INTO hits (id,ip,page,timestamp) VALUES (?,?,?,?)”)->execute(array($_GET[‘id’],$_SERVER[‘REMOTE_ADDR’],

The e-mail:

Hi Mom!
<img src=”” alt=”Open Analytics” height=”1″ width=”1″ />

And there you have it.  A simple application that will tell you every time your mom opens your message.  Whenever you want to track a new message just make up a new 6 character filename and use the code again.

Tagged as: , , , , , ,

Email this author | All posts by

9 Responses »

  1. Hey Chad,
    It would be great if you made a soltuion all ready to go. I got lost with all the different options. (How does the last part track hits when it goes through

    But, thanks for the start. Going to do some more googleing.

    – Jim

  2. @servpro
    I actually am working on a ready-to-go solution over at and I’m finishing up the documentation for Open Analytics before it’s released to the wild. You’re welcome to email me if you have any questions about making your own tracking script or want to test one of mine.

  3. Is there ready-to-solution by any chance? I like the idea of tracking the visistors and providing a graph for visual aid. Nice work!


  4. Someone recently asked for a sample script to track emails. Try it out if that’s what you’re here for.

    Included is an htaccess file which will make it so you can use images like:

    <img src="; alt="" />

    A generator that will create random strings to use with the tracker ( 32 chars using 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_., )

    And the tracking script. Configure your database at the top of track.php and it will create the necessary tables for you.

  5. thanks for a great guide!

  6. Great info–worked like a charm, thanks. I hadn’t thought of using .htaccess to parse the unique key.

  7. I was wondering if this would also work if sending an e-mail message via facebook. If it will also track if read?

  8. HI, we have been contacted by a client who is being cyber stalked and they need to find out who is doing it, so to start it would be helpful if we could have her send an e-mail with a hidden code that would reveal their ip address and anything else to help us locate the subject and put a stop to the harassment – any ideas?

  9. I suppose you are tracking all of us, too…


Leave a Reply