Simple website hit counter

Mushy-pea 0 Tallied Votes 184 Views Share

This is my first working perl script that does anything useful; that being to count the hits made on a web page and display the count graphically. To make the counter work on your web page (should you wish to), you will need to create images for the number tiles like the one found on my site. You will also need to set up a text file containing the starting hit count in the form "1\n2\n3\n4".

#!/usr/bin/perl

#This section writes a chunk of "static" HTML; i.e. HTML that is the
#same on each hit
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">\n";
print "<html>\n";
print "<head>\n";
print "<meta name=\"keywords\" content=\"Rochdale, Rochfest, music, festivals, bands\">\n";
print "<meta name=\"description\" content=\"This is the website of the planned Rochfest 2007 music festival in Rochdale (UK).  The festival is scheduled to take place in July 2007 with a target attendance of 5000.  There will be 30 bands playing over three days.\">\n";
print "<meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"iso-8859-1\">\n";
print "<title>Welcome to Rochfest</title>\n";
print "</head>\n";
print "<body background=\"zen.gif\">\n";
print "<p align=\"center\"><img id=\"logo\" src=\"rochfest-logo.gif\" width=\"597\" height=\"140\"></p>\n";
print "<p align=\"center\"><a href=\"index2.htm\"><img src=\"enter.gif\" id=\"enter\" width=\"200\" height=\"200\" border=\"0\"></a></p>\n";

#This section grabs data from a text file that describes the current hit count
$flag1 = 0;
open (file1, "visitor_stat.txt") || &finish_page;
unless (flag1 == 0) {die("Cannot open visitor_stat.txt");}
@data = <file1>;
close (file1);
print "<p align=\"center\">";

#This section writes the "active" HTML content based on the data just grabbed; i.e.
#<img> tags for the counter's number tiles
for ($c = 0; $c < 4; $c++)
{
if ($data[$c] == 0) {print "<img src=\"num0.gif\">";}
elsif ($data[$c] == 1) {print "<img src=\"num1.gif\">";}
elsif ($data[$c] == 2) {print "<img src=\"num2.gif\">";}
elsif ($data[$c] == 3) {print "<img src=\"num3.gif\">";}
elsif ($data[$c] == 4) {print "<img src=\"num4.gif\">";}
elsif ($data[$c] == 5) {print "<img src=\"num5.gif\">";}
elsif ($data[$c] == 6) {print "<img src=\"num6.gif\">";}
elsif ($data[$c] == 7) {print "<img src=\"num7.gif\">";}
elsif ($data[$c] == 8) {print "<img src=\"num8.gif\">";}
elsif ($data[$c] == 9) {print "<img src=\"num9.gif\">";}
}
print "</p></body></html>\n";
#Next, the text file needs to be updated with an incremented hit count.
$hit_count = 0;
&char_2_int;
$hit_count++;
$visitor_data = sprintf("%u", $hit_count);
&format_data;

$lockfile = "lockfile1.txt";
while (-e $lockfile)
{
sleep 1;
}
open (LOCK, ">$lockfile") || die("Cannot open lock file");
close (LOCK);
open (file2, ">visitor_stat.txt");
for ($c = 0; $c < 4; $c++)
{
print file2 "$entry[$c]\n";
}
close (file2);
unlink ($lockfile);

sub finish_page{
$flag1 = 1;
print "</body></html>\n";
}

#This function converts a number in string form (e.g. "1234"), to it's integer
#equivalent (i.e. 1234).
sub char_2_int{
local ($c, $d, $e);
for ($c = 0; $c < 4; $c++)
{
$d = 3 - $c;
$e = 10**$d;
if ($data[$c] == 0) {}
elsif ($data[$c] == 1) {$hit_count = $hit_count + (1 * $e)}
elsif ($data[$c] == 2) {$hit_count = $hit_count + (2 * $e)}
elsif ($data[$c] == 3) {$hit_count = $hit_count + (3 * $e)}
elsif ($data[$c] == 4) {$hit_count = $hit_count + (4 * $e)}
elsif ($data[$c] == 5) {$hit_count = $hit_count + (5 * $e)}
elsif ($data[$c] == 6) {$hit_count = $hit_count + (6 * $e)}
elsif ($data[$c] == 7) {$hit_count = $hit_count + (7 * $e)}
elsif ($data[$c] == 8) {$hit_count = $hit_count + (8 * $e)}
elsif ($data[$c] == 9) {$hit_count = $hit_count + (9 * $e)}
}
}

#This function copies the contents of a string variable into a character array, to
#allow easy indexing later.
sub format_data{
local ($c);
@entry = ("a", "b");
for ($c = 0; $c < 4; $c++)
{
$entry[$c] = substr($visitor_data, $c, 1);
}
}
KevinADC 192 Practically a Posting Shark

Looks like an error/typo in this line:

unless (flag1 == 0) {die("Cannot open visitor_stat.txt");}

missing the '$' before flag1, should be:

unless ($flag1 == 0) {die("Cannot open visitor_stat.txt");}

Mushy-pea 36 What, you can change this tag?

Yes, good point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.