Viewing file: counter.cgi (3.81 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
#!/usr/local/bin/perl
#
######################################################################
# A&M Text Counter Version 2.0
# Copyright 1999 Alex Iatskovski 74642.3600@compuserve.com
# Created 02/06/99
# Last Modified 06/14/99
# Home URL http://www.anmweb.com
######################################################################
#
# This is a Text Counter script that can be used to count the visitors
# of your HTTM pages on a web. This program has improved file locking
# so your counter won't constantly reset. This package should have come
# with 2 files:
#
# 1) counter.cgi - The CGI/Perl program which does all of the work.
# 2) counter.txt - It is just a plain text file with the starting
# number of your counter, by default it is 0, but you can put there
# whatever number you wish for start.
#
# Put the script and counter.txt file into your cgi-bin directory and make
# counter.txt file (or whatever you'll call it) AND DIRRECTORY writable.
# That's all! Your script is now up and running!
#
# To call this script from your web pages simply use something like:
# <!--#exec cgi="/cgi-bin/counter.cgi" -->
#
######################################################################
#
# COPYRIGHT NOTICE
# (c)Copyright 1999 Alex Iatskovski All Rights Reserved.
#
# A&M Text Counter is FREEWARE BUT IT IS NOT PUBLIC DOMAIN! This
# program may be used free of charge by anyone so long as this copyright
# notice and the comments above remain intact. Any modification to this
# program except configuration user variables is strictly prohibited!
# By using this code you agree to indemnify A.Iatskovski from any liability
# that might arise from its use.
#
# Selling the code for this program without prior written consent
# is expressly forbidden. In other words, please ask first before
# you try and make money off of my program.
#
# Obtain permission before redistributing this software over the
# Internet or in any other medium. In all cases copyright and header
# must remain intact.
#
######################################################################
# Perl's 'use strict' pragma is a handy check to make sure you are doing
# what you think you are doing. Always
use strict;
# Make sure your Perl version is not earlier than this one
require 5.003;
######################################################################
# Define Variables
# Data file path
my $data_file = "counter.txt";
# Lock file path
my $lock_file = "file.lock";
# Done
######################################################################
###################################
### Do not edit below this line ###
###################################
# Do count!
&count_num();
#####################
### Start of subs ###
#####################
##############
sub count_num{
##############
my $count_num;
print "Content-type: text/html", "\n\n";
&file_lock();
unless (open (FILE, "$data_file")){
print "[Sorry! Can't read from the counter data file]\n";
}
$count_num = <FILE>;
chop ($count_num);
close(FILE);
$count_num++;
unless (open (FILE, ">$data_file")){
print "[Can't write to the data file! Counter not incremented!]\n";
}
print FILE "$count_num\n";
close (FILE);
&file_unlock();
1 while $count_num =~ s/(.*\d)(\d\d\d)/$1,$2/;
print "$count_num\n";
}
######################
# File Locking
# Usage: &file_lock();
######################
sub file_lock{
my $i = 1;
while(-e $lock_file){
select(undef,undef,undef,0.1);
if (++$i>60) {
&file_unlock();
}
}
open(FILE,">$lock_file");
close(FILE);
}
########################
# File UnLocking
# Usage: &file_unlock();
########################
sub file_unlock{
unlink($lock_file);
}
#####################
### END OF SCRIPT ###
#####################
|