Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
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 Script Use

How many old, unused (or under-used) CGI scripts do you have lingering on your server, taking up space?

You may not be as guilty of abandoning CGI software in lieu of newer versions as we are (we've been creating and testing software for 26 years), but odds are you've got a few dusty, unused scripts lurking about.

The question we run into is "Are we using this script for anything, and if we remove it, will it leave some form or other function adrift or broken?"

Thus today's copy and paste code to log script use was written. We share it with you.

The are a number of good reasons for knowing when your CGI scripts are used on your server. It can help identify script hijacking attempts. You can know what is abandoned and what is attached to which form on what page. And you will be likely to notice what scripts need upgrading, too.

The copy 'n paste code can be inserted into most Perl scripts, causing the scripts to append a record of information to a log file every time they run.

First, here is the copy 'n paste code. Then, I'll talk about it.

{ my $file  = "file.txt";
my @time = localtime;
$time[4]++;
$time[5] += 1900;
open Wlog,">$file" unless open Wlog,">>$file";
print Wlog "Year: $time[5] ";
print Wlog "Month: $time[4] ";
print Wlog "Day: $time[3] ";
print Wlog "Time: $time[2]:$time[1]:$time[0]";
print Wlog " — IP: $ENV{REMOTE_ADDR}";
print Wlog " — Referrer: $ENV{REFERER}";
print Wlog " — Self: $0\n";
close Wlog; }

Note that the first line of the code begins with an opening curly brace character, and that the last line of the code ends with a closing curly brace character. This has the effect of isolating the code's variables from the variables in the rest of the script, negating the chance of variable name clashes.

Adding the code to your existing CGI scripts:

Always make a backup copy of any script before modifying it.

Putting the copy 'n paste code immediately below the first line of the file should work for most scripts.

As usual, do all Perl script edits with a plain text word processor like NotePad or TextWrangler. FTP file transfers must likewise be as plain text.

Once the modified script has been uploaded, run it to verify the log file is created.

If no log file is created when the script runs, your server might not allow CGI scripts to create files in the directory where the script is running. In that case:

  1. Create a subdirectory for the log file, giving it correct permissions. (Try 755 first, then 766, and finally 777.)

  2. Change the first line of the copy 'n paste code, replacing "file.txt" with the subdirectory name and the file name. Example: "log/file.txt"

The script should now create a log file (providing the subdirectory permissions are correct.)

When the script runs, it tries to open the log file. If unsuccessful, it creates the file.

Next, it grabs the server time and adjusts the month and year numbers.

Then, it appends a record to the file, one line per record.

The year, month, day, and clock time are first in the record. Next, the IP address, referrer (if available), and the location of the script itself.

Last, the file is closed.

The referrer information can help identify the form(s) that use the script. Not all browsers will provide referrer information, but many will.

When you view the log file with your FTP program, the file's date and time will let you know the last time the script was used.

Opening the file will provide you with a record of each script launch.

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