Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryStatistics and Tracking

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Log File Hit Counter

If your hosting company lets you have your own log files, a lot of good information can be extracted. A few examples:

  • How many times Google.com has referred visitors to your site.

  • How many times favicon.ico was loaded, an indication of how many times pages at your site were bookmarked using certain versions of browser (IE 5+).

  • How many times index.html was displayed compared to contact.html

  • If you have separate referrer logs, count how many times your intra-site links were clicked by searching for your domain name.

The script at the end of this article lets you do exactly that. (Although it was designed for log files, it can extract information from any plain text file.)

Some hosting companies keep access, referrer, and user agent logs all in one file. This file is often named access_log; however, the name can be anything the server is configured to use. If those three types of information are kept in separate log files, they are often named access_log, referrer_log, and agent_log, respectively.

You tell the script what to search for and click the button. It will return a count of how many log entries contained a match. If you tell it to, the script will also print every matched entry.

You can search for two terms simultaneously, if you want. Tell the script if a match occurs when either term is found or if both have to be found to be a match.

More examples:

  • Search for a question mark and display matched lines. This will give you good information about what search terms people use (for those search engines that provide that information when they refer visitors).

  • Use both a question mark and the domain name of a search engine, like altavista.com, to glean search terms used at individual search engines.

  • Use unique URLs, the destination web page file name followed by # and a code, when you place ads. Then search for that file name and code to get a count of responses. The same technique can be used to see how many people click on certain intra-site links. For example, if you want to know how many people click on your sales page from your index page, put this link on the index page:

    <a href="sales.html#fromindex">Click</a>
    
    
    -- now, you can search for "sales.html#fromindex" in your log file and get an exact count of clicks.

  • If you suspect someone is linking to graphics on your site, search for their domain name with lines matched to be displayed. If they're doing it, the evidence will be right in front of you.

Below is the script. Copy it and give it whatever file name makes sense to you. Example: logreader.cgi

Ensure the first line of the script points to the location of perl on your server. The second line must contain the location of your log file between the apostrophes.

Upload the script as ASCII/plain text with an FTP program, into a directory allowed to run Perl CGI scripts. Give the script global execute permissions.

To run the script, type its URL into your browser. Example: http://domain.com/cgi-bin/logreader.cgi

When the script runs, it will present you with a control panel where you specify your search terms and conditions.

The next 90+ lines is the script:

#!/usr/bin/perl
my $LogFile = '/www/httpd/logs/access_log';
use strict;
my ($Q1,$Q2,%In) = ();
my $SearchTermsCount = 0;
my $ME = $0;
$ME =~ s!^.*[\\/]!!;

sub Parse
{
my $buffer;
read(STDIN,$buffer,$ENV{CONTENT_LENGTH});
my @p = split(/&/,$buffer);
foreach(@p)
{
$_ =~ tr/+/ /;
my ($n,$v) = split(/=/,$_,2);
$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$In{$n} = $v;
}
} # sub Parse

sub Match
{
my $s = shift;
my($one,$two) = (0,0);
$s =~ /($Q1)/i;
my $one = $1;
/()/;
$s =~ /($Q2)/i;
my $two = $1;
if($In{choice} eq 'e' or $SearchTermsCount < 2)
{ return ($one or $two); }
else
{ return ($one and $two); }
} # sub Match

print "Content-type: text/html\n\n";
print '<html><body bgcolor="white">';
Parse;
if($In{choice})
{
$Q1 = quotemeta $In{one};
$Q2 = quotemeta $In{two};
print '<h3>Searching for ';
$SearchTermsCount++ if $In{one};
$SearchTermsCount++ if $In{two};
if($SearchTermsCount > 1)
{
if($In{choice} eq 'e') { print 'either'; }
else { print 'both'; }
print " "$In{one}" ";
if($In{choice} eq 'e') { print 'or'; }
else { print 'and'; }
print " "$In{two}"";
}
else
{
if($In{one}) { print ""$In{one}""; }
else { print ""$In{two}""; }
}
print '</h3>';
my $counter = 0;
if($In{one} or $In{two})
{
print '<pre>' if $In{display};
open R,"<$LogFile";
while(my $line = <R>)
{
if(Match($line))
{
$counter++;
print $line if $In{display};
}
}
close R;
print '</pre>' if $In{display};
}
print "<h3>Count: $counter</h3>";
print '<p><hr width="25%"></p>';
}
print <<FORM;
<div align="center">
<form action="$ME" method="POST">
<table border="0" cellpadding="0" cellspacing="0"><tr><td>
Look for:<br>
<input type="text" name="one" size="44"><br>and
<input type="radio" name="choice" value="e" CHECKED>Either 
<input type="radio" name="choice" value="b">Both<br>
<input type="text" name="two" size="44"><br>
<input type="checkbox" name="display" value="yes" CHECKED>
Display match lines
</td></tr></table>
<input type="submit">
</body></html>
FORM
# end of script

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC