Below are two very simple examples of creating a Hit Counter.

This simply increments each time a user visits a site.  This is the first “Server Side” code I ever wrote, albeit in perl at th


1. The Text Counter

Page Hit counters may be easily created for your web pages. A text counter is the simplest method of counting visitors to a page. Such a counter is easily created using PHP. The Hit counter looks like this: 373 (Click Refresh on your browser to watch it increase)

For this to work your .htm or .html file must be changed to have the extension .php You should also have a text file called count.txt which contains nothing but the count (No line breaks etc) e.g.e.g. 000001

At the point on the page where you wish the counter to be placed, enter the following code:

1.) Text Counter using PHP
<?php$id = ‘count.txt’;                         // File Which stores the count

$fp = fopen($id,”r”);

$count = fgets($fp, 4096);                 // Get a number from the file


$count++;                                  // Increment it

$fp = fopen($id,”w”);                      // Put it back into the file

fwrite($fp, “$count”, 4096);


print $count;


The count.txt file should be readable and writable by all. e.g. CHMOD 666. If using CuteFTP, this is acomplished by right clicking the uploaded file, and choosing chmod. All rows of the first TWO columns should by checked in the popup box that appears. Now simple upload your php file and watch the counter go.

2. The Graphical Counter

In order for this to work, your ISP must have installed the GD library for GIF Images. This uses a modification of the above script to create a graphical counter. It is advisable to be familiar with how the above script works before attempting this script.

The following are required: A Suitable background Image (must be a .gif). A text file for holding the counA text file for holding the count as with the text counter.

The following code inserted into a file called count.php

2.) Graphical Hit Counter using PHP.
<?php$id = ‘count.txt’;                           // File Which stores the count

$fp = fopen($id,”r”);

$count = fgets($fp, 4096);                   // Get a number from the file


$count++;                                    // Increment it

$fp = fopen($id,”w”);                        // Put it back into the file

fwrite($fp, “$count”, 4096);


Header(“Content-type: image/gif”);           // Tell the Browser we’re sending a gif

$im = imagecreatefromgif(“count.gif”);       // Load the background gif file

$black = ImageColorAllocate($im, 0, 0, 0);   // We want black text

$px = (imagesx($im)-7.5*strlen($count))/2;   // Center the count

ImageString($im,3,$px,9,$string,$black);     // Write the text

ImageGif($im);                               // Send it to their browser

ImageDestroy($im);                           // Delete the temporary file


The counter may then be called from a standard image tag such as:

<img src=”count.php”>

Please note that all code examples are provided “as is”, without any kind of warranty.