found the world simplist perl counter script, i could written this one myself in about 10 minutes.. i would have probably spent another 10 minutes to count only unique ip's though..
#!/usr/bin/perl -w
use strict;
use CGI qw/:all/;
# This is the path to the file that will contain the
# counter hits, chmod this file to 666
my $countdat = "/usr/share/apache/www/counter/count.dat";
# An alternative to using CGI header is to specify the
# content type manually by uncommenting the next line
# print "Content-type:text/html\n\n";
print header;
# If the count.dat file exists, is readable and writable
# open it and store the number in a variable called $count
if ( (-e "$countdat") && (-r "$countdat")
&& (-w "$countdat") )
{
open(DAT, "$countdat") || die "Problem: $!";
my $count = <DAT>;
close(DAT);
# Add 1 to this number (increment the number of hits)
$count++;
# Update the count.dat file with the new value
open(COUNT, ">$countdat") || die "Problem: $!";
print COUNT $count;
close(COUNT);
print "$count";
} else {
print "the counter file is not readable or writable\n";
}
- #!/usr/bin/perl -w
- use strict;
- use CGI qw/:all/;
- # This is the path to the file that will contain the
- # counter hits, chmod this file to 666
- my $countdat = "/usr/share/apache/www/counter/count.dat";
- # An alternative to using CGI header is to specify the
- # content type manually by uncommenting the next line
- # print "Content-type:text/html\n\n";
- print header;
- # If the count.dat file exists, is readable and writable
- # open it and store the number in a variable called $count
- if ( (-e "$countdat") && (-r "$countdat")
- && (-w "$countdat") )
- {
- open(DAT, "$countdat") || die "Problem: $!";
- my $count = <DAT>;
- close(DAT);
- # Add 1 to this number (increment the number of hits)
- $count++;
- # Update the count.dat file with the new value
- open(COUNT, ">$countdat") || die "Problem: $!";
- print COUNT $count;
- close(COUNT);
- print "$count";
- } else {
- print "the counter file is not readable or writable\n";
- }
Script explanation - SSI:
Files that contain SSI code to be executed normally have an extension such as '.shtml' - This can be changed, but is out of the context of this article.
In order for you to run SSI, you will need to rename your '.html' file to '.shtml'. The line that you need to add to your file from which the counter will be used is:
<!--#exec cgi="/path/to/cgi/counter.pl"-->
Now, everytime your page is loaded, the counter is incremented and the current value is displayed instead of the line between the <!-- and the -->.