Perl Hit Counter Script Explanation
Line By Line... hits.pl
This script is a basic page hits counter. It stores the hit count in a database table called 'hits'...
- The first line tells Unix this is a Perl program. The second is a comment describing the script.
#!/usr/local/bin/Perl
# hits.pl - V1.0 - TGH 26 May 2001
- This makes the DBI (DataBase Interface) library of functions available.
use DBI;
- Connect to the database.
$dbh = DBI->connect("DBI:mysql:yourname:localhost:3306", 'yourname', 'password');
- Create the SQL that will increment the counter for the page. The special variable '$_[0]' is the first parameter that was passed to the script - in this case the web page name.
$statement = "update hits set counter = counter + 1 where page = $_[0]";
$sth = $dbh->prepare($statement) or print "Can't prepare the SQL\n";
- This runs the SQL on the database. the variable $rv contains the number of rows inserted!
$rv = $sth->execute or print "Can't execute the query: $sth->errstr";
print $rv, " rows inserted\n";
- Close the 'statement handle' and disconnect from the database.
$sth->finish;
$dbh->disconnect;
|
|