Hit Counter
 

A simple PHP script that allows you to keep track of the number of hits your site gets. All the program does is keep track of the number of hits in a separate file, each new hit just adds one to the value in that file. Its a very simple thing to do, all you need to do is copy this code into your web page and then download the zip of all the images and unzip them into a folder. Change the location in the script to be the folder where you put them, and you're done. All you need is for your server to have PHP enabled for this to work.

Download the images here.

Code

<?
// Hit Counter
// Uses images and a table to create a hit counter
$location = "images"; // Location of images, relative to this file

// Print the leading information for the table
print "<table width='125' border='0' cellspacing='0' cellpadding='0' align='center'>\n";
print "<tr>\n";
print "<td><img src='$location/border1.bmp' width='9' height='9'></td>\n";
print "<td><img src='$location/border2.bmp' height='9' width='107'></td>\n";
print "<td><img src='$location/border3.bmp' width='9' height='9'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td><img src='$location/border4.bmp' width='9' height='22'></td>\n";
print "<td>\n";

// Calculate the number of hits, and increment it bye one
$hits = 0; // Number of hits
if(file_exists("counter.dat")) // Check for counter file
{
// File exists, read the number of hits from the file
$exist_file = fopen("counter.dat", "r");
$new_count = fgets($exist_file, 255);
$new_count++; // Increase count by one
fclose($exist_file);

$hits = $new_count; // Save the number of hits

// Write the number of hits to the file
$exist_count = fopen("counter.dat", "w");
fputs($exist_count, $new_count);
fclose($exist_count);
}
else
{ // File does not exists, create file
$new_file = fopen("counter.dat", "w");
fputs($new_file, "1"); // Write 1 to the file, first hit
$hits = 1;
fclose($new_file);
}

// Extract the digits from the file
while($hits != 0) {
$digit[] = $hits % 10;
$hits = floor($hits / 10);
}

$size = count($digit); // Number of digits
$count = 6; // Counter, always need 6 numbers
while($count != 0) {
if($count > $size) {
// Less than 6 digits, display leading 0's
$count--;
print "<img src='$location/0.jpg' width='17' height='22'>";
} else {
// Display the image corresponding to the digit
$count--;
print "<img src='$location/$digit[$count].jpg' width='17' height='22'>";
}
}

// Print the remainder of the information for the table
print "</td>\n";
print "<td><img src='$location/border5.bmp' width='9' height='22'></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td><img src='$location/border6.bmp' width='9' height='9'></td>\n";
print "<td><img src='$location/border7.bmp' width='107' height='9'></td>\n";
print "<td><img src='$location/border8.bmp' width='9' height='9'></td>\n";
print "</tr>\n";
print "</table>\n";
?>

Example