|
Tutorial 3 - Web Hit Counter Using an Image Tag
|
|
The Program:
|
#!/usr/local/bin/perl
#
# Webpage Hit Counter
#
# Program03 in a series of CGI/Perl Tutorials on
# http://www.thecgibin.com/
# Author : Marty Landman Email : cgiperl@thecgibin.com
#
# You may use and distribute this freely as long as you agree
#
# 1) not to hold Face 2 Interface Inc or Marty Landman
# responsible for the results of using this software
# 2) to keep these comments intact
#
use strict;
&count;
&prtpx;
sub count {
my $count = 'data/article03.dat';
open (COUNT, "+<$count") or die "cannot open countfile: $!";
flock(COUNT, 2);
my $counter = <COUNT>;
$counter++;
seek (COUNT, 0, 0) or die "seek failed : $!";
print COUNT "$counter";
flock(COUNT, 8);
close (COUNT) or die "cannot close countfile: $!";
return($counter);
}
sub prtpx {
my $image = 'a.gif';
open(IMAGE, "<$image") or die "cannot open image file: $!";
my $no_bytes = (stat ($image))[7];
print "Content-type: image/gif\n";
print "Content-length: $no_bytes\n";
print "Pragma: no-cache\n\n";
print <IMAGE>;
close(IMAGE) or die "cannot close image file: $!";
}
__END__
|
|
|
|