Web page counter example

#!/usr/local/bin/perl -- -*- perl -*-
#
# counter.pl
# This script acts a page counter for web pages
# Written by Graeme Cross, 4/12/95 - v1.02
#

require 'cgi-lib.pl';

# set up the counter database
$database = "counter.db";

# initialise the hit counter
$count = 0;

# what is the URL of the page calling the counter?
$page = $ENV{'DOCUMENT_URI'};

# open the database - it will be created if it doesn't exist
dbmopen(%DATA, $database, 0644) || die "Error: Can't open $database!";

# search to see if we already have an entry for the URL in the database
# if we do, grab the hit count for that URL (otherwise it will be zero)
while (($key, $value) = each(%DATA)) {
  if ($key eq $page) {
    $count = $value;
    last;
  }
}

# increment the counter and store it in the database
$count++;
$DATA{$page} = $count;
dbmclose(%DATA);

# print out the information to the web page
print &PrintHeader;
print "$count";



Perl & Linux Updated: 4 December, 1995