|
|
Creating a Page Counter In Perl
You are here: irt.org | Articles | CGI & Perl | Creating a Page Counter In Perl Published on: Sunday 18th July 1999 By: Jason Nugent
IntroductionWell, I know I've been gone for a while, but that's because Real Life got extremely busy all of a sudden. At any rate, I'm back with a new Perl article. I've decided to write an article on this topic because it seems that fully 80% of all the questions I've received as of late have been about this topic. Without further ado, here goes. My rant about CountersGenerally, page counters don't really offer a webmaster any useful information that they cannot get through some other means. You *do* have your server logs, and they contain more information that could possibly be represented by a single number on the bottom of your page. Counters are pretty much only good for vanity, and please, don't use them if you only get 5 hits a week. Not worth it. Anyway - on to the CounterThis article describes a counter that is going to be run as a Server Side Include. Essentially that means that we are going to write a Perl script that will run each time the page is loaded and its outputted HTML will contain a line of text indicating how many people have seen the page. It sounds pretty simple, and it is. Let's get started. Counter.pl - the Good StuffAs always, the first line in your Perl script must be a locator line pointing to the location of the Perl interpreter installed on your server.
Remember, we add the -w to turn on extra warning information to help us debug our script. Essentially, this script is going to open a text file on the server, read something from it (the number of visitors prior to this visit), increment a number, and print something to STOUT. To read a file, we use the following syntax:
The above code also makes a few concessions we've discussed in previous articles. It opens the file in read/append mode so we can read the contents in and still overwrite it, and it locks the file in case something else tries to access it before we have completely finished with it. We also call 'die' to ensure that if something goes wrong, our script doesn't continue to run and cause problems. Once we have our file opened, we must move to the beginning of the file so we can read in the file's contents (since opening it in append mode automatically puts the pointer at the end of the file). We do this in the following manner:
and then we read the file's contents into an array.
Now that we have the files contents, we can truncate the file to zero size so we can overwrite it once we know what the new counter amount will be. We do this using the 'truncate' command:
Essentially, all we have to do now is extract our number out of our array and increment it. I will use a regular expression for this, even though you could quite conceivably do it by simply grabbing the first element of the array without checking since there most likely won't be anything but a number here. Using a regular expression gives you the opportunity to do some error checking, which makes it a nice way to go about it.
For the rest of the article, we'll assume that the regular expression matched. Normally, you would check to see if it did or not, and perform appropriate error handling at that point. Now that we have our counter value, we can increment it, store it in our file, and print it to the browser:
To store it in the file, we just print to the filehandle and close our file.
To get this to work, all you need to do is insert an exec Server Side Include in your page and make sure you have SSI enabled in your webserver. There are ways to get around using SSIs that are better alternatives, but I'm not going to get into them just yet. Maybe in a future article ;) Something like this works just fine:
Where /cgi-bin/counter.pl is the actual path to your counter perl script. Note, that you NEED the "exec" SSI enabled. Most webservers that have SSI enabled probably have this one turned off, since it is a security risk. If you have questions about that, talk to your sys admin. The Complete Code
Feedback on 'Creating a Page Counter In Perl'
View the profile on Jason Nugent and the list of other Articles by Jason Nugent. |
-- div -->
|