#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use Fcntl qw(:flock :seek);

print header;           # print the content-type header

open(IN,"+<counts") or &dienice("Can't open counts for reading: $!");
flock(IN,LOCK_EX);	# lock the file (exclusive lock)
seek(IN,0,SEEK_SET);	# rewind it to the beginning
my $count = <IN>;       # read only the first line.

$count = $count + 1;    # increment the counter

seek(IN,0,SEEK_SET);	# rewind it to the beginning
print IN "$count\n";   # write out the new count
close(IN);             # close the file.

print qq(<p><img src="imgcount.cgi?$count"></p>\n);

sub dienice {
    my ($errmsg) = @_;
    print "<p>$errmsg</p>\n";
    exit;
}