Sunday, March 2, 2008

Web Server Log Element Reporting Using CGI

Hey there,

Today's post is another quick and simple CGI script, written in Perl for Unix or Linux. It should be noted at the outset that, even though I like to do this sort of thing for my own "ease of use," it's nothing I actually endorse doing in any sort of an environment where security is a factor.

The script we're presenting today is fairly simple and also fairly simple to compromise. It basically goes against one of the main tenets of our post on processing emails with CGI. For security's sake, you should "never" allow direct access to any file, or initiate any process, directly through a simple CGI page. This opens you up to a thousand different ways a user can insert form values and, possibly, read your local files and/or run processes you didn't want them to on your local machine!

In any event, for experience's sake, just create this as a .pl or .cgi file and load it up in your web browser (be sure to set execute permissions on it - 755 should cover you) and enjoy your page element counts. You can change the main variables as much as you like, but this script is just looking at how many times our user base looked at pictures (jpg's and gif's) as compared to HTML pages. The possibilities are only limited to your imagination. And your willingness to take this risk ;)

Tomorrow, we'll look at doing this without CGI, using the bash shell instead of Perl. Yes, even more porting examples leading up to the series of concept articles :)


Creative Commons License

This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


# Count what people are accessing on your site
# 2008 - Mike Golvach -
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

print "Content-type: text/html\n\n";
$access = "/wherever/your/http/logs/are/access_log";


open(ACCESS, $access) or die "Can't open access log!\n";

while(<ACCESS>) {
$html++ if /\.html/;
$gif++ if /\.gif/;
$jpg++ if /\.jpg/;

print "<html>\n";
print "<head><title>Page hits</title></head>\n";
print "<body>\n";
print "$count pages accesssed - Form Elements Processed:\n";
print "$html html pages accessed.\n";
print "$gif GIF files requested.\n";
print "$jpg JPEG files requested\n";
print "</body>\n";
print "</html>\n";

, Mike