#!/usr/bin/perl use DBI; print "Content-type:text/html\n\n"; $dbh = DBI->connect( "dbi:mysql:products", "webserver", "", {RaiseError=>1,AutoCommit=>1} ) or &dienice("Can't connect: $DBI::errstr"); $uri = $ENV{'REQUEST_URI'}; if ($uri eq "") { exit; # don't update a blank counter. } # remove index.html from the end of the URI, so that "/class/ch18/index.html" # becomes "/class/ch18/". if ($uri =~ /(.*)index.html/i) { $uri = $1; } $sth = $dbh->prepare("select count from counts where pagename=\"$uri\""); $sth->execute or db_err("Unable to execute query", $dbh->errstr); ($count) = $sth->fetchrow; if ($count > 0) { $count++; $sth = $dbh->do("update counts set count=count+1 where pagename=\"$uri\"") or db_err("Unable to execute query", $dbh->errstr); } else { $count = 1; $sth = $dbh->do("insert into counts values(\"$uri\",1)") or db_err("Unable to execute query", $dbh->errstr); } print "You are visitor number $count.\n"; sub dienice { ($msg) = @_; print "