#!/usr/local/bin/perl # *********************************************************************** # # * * # # * Copyright (c) 1996-1999 Scott Crevier * # # * All Rights Reserved Worldwide * # # * * # # * This program product material is the property of Scott Crevier. * # # * This program may be used and modified by anyone free of charge, * # # * as long as this copyright notice remains in tact. By using this * # # * code, you agree to indemnify Scott Crevier from any liability * # # * that might arise from its use. * # # * * # # * scott@crevier.org - Scott M. Crevier - www.crevier.org * # # *********************************************************************** # # * smcCount v2.0 21-Dec-1998 * # # * * # # * This program displays a simple text counter. The filename used to * # # * store the counter value is based on the name of the html file * # # * that called this script. Therefore, many different html files can * # # * use this counter script and each one will have its own place to * # # * store the value. The nice thing that I like about this method is * # # * explained in the following example: * # # * * # # * If my 'index.shtml' file uses this counter, the value will be * # # * stored in the file 'index.shtml.cnt'. This allows me to include * # # * the following code in my 'index.shtml': * # # * * # # * This page was last accessed on * # # * * # # * * # # * As you can see, I am displaying the last modification time of the * # # * counter file. Obviously, in order for this to work properly, you * # # * need to do this before actually executing the counter script. * # # * * # # * Suggested usage: * # # * * # # * or * # # * * # # * * # # *********************************************************************** # # *********************************************************************** # # * Get the name of the file where the counter value is stored. * # # * This will be the name of the file that called this script with * # # * an extension of ".cnt". This file will also be stored in the same * # # * directory as the file that called this script. * # # * So, for example, if the file "index.shtml" called this script, * # # * then the counter will be stored in "index.shtml.cnt". * # # *********************************************************************** # $filename = $ENV{'DOCUMENT_ROOT'} . $ENV{'DOCUMENT_URI'} . ".cnt"; # *********************************************************************** # # * If the counter file exists, we increment it and write it back. If * # # * the counter file does not exist, we try to create it. The ability * # # * to create the file will simply depend on the permissions of the * # # * directory where the counter file will reside. If the file cannot * # # * be created by this script, you'll have to create it manually. The * # # * following two commands will do that just fine: * # # * * # # * touch index.shtml.cnt * # # * chmod a+w index.shtml.cnt * # # *********************************************************************** # if (-e $filename) { #**************************************************************# #* The counter file exists so we need to increment it. *# #**************************************************************# if (open (FILE, "+<$filename")) { #******************************************# #* Lock the counter file. *# #******************************************# flock (FILE, 2); #******************************************# #* Get the current counter value. *# #******************************************# $counter = ; #******************************************# #* Write the incremented value back to *# #* the file. *# #******************************************# seek(FILE,0,0); print (FILE ++$counter); #******************************************# # Release the lock, then close the file. *# #******************************************# flock (FILE, 8); close (FILE); } else { #******************************************# #* This means that the file exists but *# #* we do not have permission to write. *# #******************************************# print "Sorry pal, can't write to file '$filename'. Counter not incremented!", "\n"; } } else { #**************************************************************# #* The counter file does not exist so we need to create it. *# #**************************************************************# if (open (FILE, ">" . $filename)) { #******************************************# #* Lock the counter file. *# #******************************************# flock (FILE, 2); #******************************************# #* Set the counter to 1. *# #******************************************# $counter = 1; #******************************************# #* Write the value back to the file. *# #******************************************# print (FILE $counter); #******************************************# # Release the lock, then close the file. *# #******************************************# flock (FILE, 8); close (FILE); } else { #******************************************# #* This means that the file does not *# #* exist and we do not have permission *# #* to create it. *# #******************************************# print "Sorry pal, can't create file $filename. Counter not incremented!", "\n"; } } # *********************************************************************** # # * Print the counter value. * # # *********************************************************************** # print "Content-type: text/plain", "\n\n", $counter; exit (0);