BW
Main Menu
Home
AMTP
Bio
Hire Bill
Bill’s Music
BW Whois
Boulder Pledge
UBE-Related
Blog
CGI Scripts
CMS Project
Music DB
Creative HTML
The CGI Book
Perl Book
BillyDos
Privacy
Newsletter
Contact Bill
The End
· Sponsors ·
amazon.com
Personal Loans Online
Names Database
Google Optimization
Free Credit Score
Learn Programming
Live Music
Rheumatoid Arthritis
Mortgage Leads
Payday Advance
Debt Consolidation
Bad Credit Loans
Mortgage Refinance
Newsletter Delivery
Mobile Ringtones
Home Loans
Netflix DVD Rentals
Site Design: Bill Weinman
© 1995-2008
W.E. Weinman

CGI Scripts by Bill Weinman

bw-counter

This is a simple hit counter for your web page. It is run from a server-side include (SSI).

On an Apache server you put something like this in your page:

  <!--#include virtual="/bw-counter/bw-counter.cgi" -->

The result should be something like this:

46,597

You will need to create a file for the count, and make sure the web server has read and write permission for that file (i.e., chmod 666 filename on a Unix system). The full path to that file should go in the $count_file variable near the top of the script.

Here is the source code for bw-counter.cgi. Copy/paste this code into your text editor, and make sure that the line

#!/usr/bin/perl -w
is the first line in the file.


#!/usr/bin/perl -w
# 
# bw-counter.cgi 
#
# Create an incremental counter for a web page. 
#
# Copyright 2003 by Bill Weinman [http://bw.org/]
#
# The author grants permission to use and modify this program as you like. 
# All other rights and ownership are reserved by the author. 
#

use strict;
use IO::File;
use Fcntl ':flock';

my $count_file = "/full/path/to/count";

# minimal valid mime header
print "Content-type: text/html\n\n";

my $count = readfile($count_file);
chomp $count;
$count ++;
print comma($count);
writefile($count_file, "$count\n");

exit;

sub error
{
print shift;
exit;
}

sub comma
{
my $number = shift;
$number += 0; # make sure it's a number;

while ($number =~ s/ ( .*\d ) ( \d \d \d ) /$1,$2/xg) { }
return $number;
}

sub readfile
{
my $filename = shift or return;
my $f = new IO::File("<$filename");
error("cannot open $filename ($!)") unless $f;
my $string = join('', <$f>);
$f->close;
return $string;
}

sub writefile
{
my $filename = shift or return;
my $data = shift || '';
my $f = new IO::File(">$filename");
error("cannot write $filename ($!)") unless $f;
flock($f, LOCK_EX);
$f->print($data);
flock($f, LOCK_UN);
$f->close;
}

How does Michael Jackson pick his nose? From a catalog!
·/.