#!/usr/bin/perl ########################################### # Whos On Now? v1.0 by Allen C. Huffman # # Copyright (C) 2001 by Sub-Etha Software # # FREEWARE! # # subetha@pobox.com # # http://disneyfans.com/subetha # ########################################### # # You may do whatever you want with this code, provided you put a comment in that # says "Baseed on" (or similar) and my original copyright notice above. # # Revision History: # # 2001/08/05 1.0 Initial (working) version. # # 2001/08/07 Christmas mods by Chris Overstreet (chris@lettersfromsanta.com) # to show number of "elves" online (http://www.lettersfromsanta.com) # # # Specify how many SECONDS has to pass before a visitor is considered no longer # on the site. For instance, setting the value to 60 will show the count of # how many times the page has been loaded within the past 60 seconds. # $timespan = 300; # # If you want the display to show the timespan ("in the past X seconds/minutes") # then set this value to 1. If it is set to 0, the output will read "currently # on the site". # $showtimespan = 0; # # Define the path and file name for the data file. # $whosonnow_file = "sewhosonxmas.dat"; #################################################################################### # # Begin processing... Nothing below this line needs to be modified. # print "Content-type: text/html\n\n"; # get the IP address of the current page visitor. $currentip = $ENV{REMOTE_ADDR}; # if no IP, use a bogus one if ( !$currentip ) { $currentip = "127.0.0.1"; } # open (or create) the visitor log file. unless ( open(FILE, "+<$whosonnow_file") ) { open( FILE,"+>$whosonnow_file") || die ("Unable to create file.\n" ); } # read the entire contents into an array. @file = ; # get the current system time (in seconds) $currenttime = time; # back up to start of file and erase it seek(FILE,0,0); truncate(FILE,0); # scan through previous visitors until we find one that has # visited within the specified amount of time $visitors = 1; # added variables for tense # Chris Overstreet (chris@lettersfromsanta.com) 08/07/01 $elftense = "elf"; $thereis = "There is"; $therehas = "There has been"; # check each entry of the log file. foreach $line (@file) { # split the line into ipaddress and timestamp. ( $ipaddr, $timestamp ) = split(" ",$line); # is this an old entry or a repeate visitor within the timespan? unless ( ($timestamp < $currenttime-$timespan) || ($ipaddr eq $currentip) ) { # this is within the specified period, so count it... $visitors++; # then save it back to the file. print FILE "$ipaddr $timestamp\n"; } } # write the current IP and timestamp to the end of the file. print FILE "$currentip $currenttime\n"; # change tense variables if number of visitors is greater than 1 # added 08/07/01 Chris Overstreet (chris@lettersfromsanta.com) if ( $visitors!=1 ) { $elftense = "elves"; $thereis = "There are"; $therehas = "There have been"; } # display how many visitors are on the site. # Modified to use $therehas variable 08/07/01 # Chris Overstreet (chris@lettersfromsanta.com) if ( $showtimespan ) { print "$therehas"; } else { print "$thereis"; } print " $visitors $elftense"; # Commented out as this is superceded by above routine # 08/07/01 Chris Overstreet (chris@lettersfromsanta.com) # #if ( $visitors!=1 ) { # print "s"; #} # and, if selected, the timespan. if ( $showtimespan ) { print " online within the past "; if ( $timespan<60 ) { print "$timespan seconds."; } else { $minutes = $timespan/60; print "$minutes minute"; if ( $minutes!=1 ) { print "s"; } print "."; } } else { print " currently online."; } # just to make output easier to read when testing offline. print "\n"; close(FILE); exit(0);