![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Analyzing the Whole-Directory Hit CounterDespite what looks like an intimidating new program, you have only added two new features to your original hitcnt.pl. You have used Perls directory functions to get a handle to the log file directory and you put the code that processes the IP addresses and counts hits into a foreach loop. The changes at the top of the program are minimal: $LogDir = "c:/winnt/system32/logfiles"; opendir (LOGD, $LogDir) || die ";CanCt open $LogDir: $!\n"; @LogFiles = readdir (LOGD); Notice that the only string constant you need to define now is the path to the log directory. The string, $LogDir, is the argument to opendir, which returns the LOGD handle. The handle is used by readdir to put all of the directorys files into @LogFiles. There are two items of special interest at the top of the foreach loop. The first: foreach $LogFile (@LogFiles) { if (($LogFile eq ".") || ($LogFile eq "..")) { next; } tests for the two special files present in every Windows and UNIX directory. The file name . refers to the current directory and .. is the parent; we dont need them in this example, so they are ignored. How? By using the Perl next statement, which is essentially the opposite of the last statement you learned about in the previous section. Where last breaks out of the loop, next makes it loop again without executing any more of the subsequent code. In this example, if $LogFile has the value of either of the special file names, we dont want to do anything but get the next file name. The next code section prevents a common mistake in working with directory handles. $LogPath = $LogDir."/".$LogFile; open (LOG, $LogPath) || die "Cant open $LogPath: $!\n"; Notice that an entire path to the file is built into $LogPath by combining the directory name and the file name. Why? Because $LogFile will work all by itself to open the file only if you happen to have the log directory set as your current directory, too. The chances of this being true every time you run hitcnt1.pl are quite slimits wisest to specify the full path to the file. The last new feature in hitcnt1.pl makes a date out of an IIS log file name. if ($LogFile =~ /(..)(..)(..)(..)/) { $year = $2; $month = $3; $day = $4; } print "On $month/$day/$year:\n\n"; Remember how daily IIS log names are formatted? The first two characters are in followed by the two-digit year, month, and day. In other words, the log file for August 24, 1997, would be named in970824.log. The regular expression /(..)(..)(..)(..)/ matches each of the first four pairs of characters in the file name in order. Therefore, we can pull the year out of $2, the month out of $3, and the day out of $4, ignoring $1 altogether. Taking the Hit Counter to the WebYou now know how to navigate through an entire directory of IIS log files, and calculate and display the number of hits on each day from every IP address that has connected to your Web site. The last stepputting the whole thing on a Web pageis the easiest. Youve already done all the hard work in reading the log files and manipulating the data. The only changes that need to be made are in the print statement at the bottom of hitcnt1.pl. Theyll need to put out HTML-formatted code now. The HTML conversion requires a few lines of additional code. First, call in your HTML header and ender code near the top: # Bring in HTML header and ender stuff. require "d:/pub/scripts/perl-cgi/html.pl"; # Define path to a single log file and a page title. $LogDir = "c:/winnt/system32/logfiles"; $Title = "Counting Web page hits from various IPs";
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |