How to Build a Hit Counter

Builders: Doug / Harris


Long ago, we began our search for a hit counter which was easy to build. No matter where we looked on the net, the only examples of creating a hit counter that we found were often cryptic and/or difficult to implement. Many involved downloading over 100 files and compiling C programs.

We decided to take matters into our own hands and wrote our own. The difference with our hit counter, though, is that ours is easy to understand and implement. It also doesn't involve C and is very, very small.



To get started, you have to cut and paste the following code into files you will create.


Change the suffix on the name of your page from .html to .shtml
This command is a server side include so the document has to have a suffix of shtml. Add the following line at the position you want your hit counter

<!--#exec cmd="cd counter;./counter.pl counter.txt counter/gifs 20 9"-->
The command:
Changes directory to the folder, relative to your .shtml document, where the counter.pl perl script is created.
The ./counter.pl executes the perl script.
(on some operating systems you may have to substitute perl counter.pl for ./counter.pl)
Following it are passed parameters.
The counter.txt passes the name of the file in the same directory as counter.pl holding the count.
The counter/gifs passes the directory path relative to your .shtml document of the directory of gifs discussed later.
The 20 and the 9 are the height and width in pixels of each number. You should change these to match the gifs you are using.


Create a directory called counter with 755 security access.


Create a file in the counter directory called counter.pl. The first line that says "#!/usr/bin/perl" should be replaced with the path to perl on your server. From a telnet session, the command which perl will give you the path or ask your administrator if you're not sure. It must have 755 security access and have the following lines of code:
#!/usr/bin/perl 

##################################################################
### Notice should be included in this program.                 ###
### Created by Harris / Doug                                   ###
###                                                © 1997      ###
##################################################################
$file   = $ARGV[0];
$dir    = $ARGV[1];
$height = $ARGV[2];
$width  = $ARGV[3];

#Get current value of counter from file
open(Counter,"<$file") || die "Can't Open Counter";
flock(Counter,2);
$currCount = <Counter>;
close(Counter);

#Increment counter and rewrite to file
open(Counter,">$file") || die "Can't Open Counter";
print Counter ++$currCount;
close(Counter);
flock(Counter,8);
	
#Pad counter with 0's and return html code with 6 gif digits
$currCount = 0 x (6-length($currCount)) . $currCount;
print ("<img src=$dir/", substr($currCount,0,1), ".gif height=$height width=$width>");	
print ("<img src=$dir/", substr($currCount,1,1), ".gif height=$height width=$width>");	
print ("<img src=$dir/", substr($currCount,2,1), ".gif height=$height width=$width>");	
print ("<img src=$dir/", substr($currCount,3,1), ".gif height=$height width=$width>");	
print ("<img src=$dir/", substr($currCount,4,1), ".gif height=$height width=$width>");	
print ("<img src=$dir/", substr($currCount,5,1), ".gif height=$height width=$width>");	



Create a file in the counter directory called counter.txt with 666 security access. Put the number 1 in it to start the counter.


Create a directory called counter/gifs with 755 security access.


Within the counter/gifs directory, move 0-9 number images with 444 security access. Each gif should be named 0.gif through 9.gif, respectively. To get gifs to choose from, go to www.digitmania.holowww.com for a huge selection.






The following is a summary of the directory structure we use:
home
public_html
your_page.shtml
counter
counter.pl
counter.txt
gifs
0.gif
1.gif
2.gif
3.gif
4.gif
5.gif
6.gif
7.gif
8.gif
9.gif
That's it!!



Homepage

Updated: December 1, 2003