#!/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. # # # 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 = 1; # # Define the path and file name for the data file. # $whosonnow_file = "sewhosonnow.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; # 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"; # display how many visitors are on the site. print "$visitors visitor"; if ( $visitors!=1 ) { print "s"; } # and, if selected, the timespan. if ( $showtimespan ) { print " 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 on the site."; } # just to make output easier to read when testing offline. print "\n"; close(FILE); exit(0);