Cannot open $counter_file: $!\n
", die "Cannot open $counter_file: $!\n
"); if ($flock){ flock (COUNTERREAD, 1) } read(COUNTERREAD,my $buffer, -s $counter_file,0); if ($flock){ flock (COUNTERREAD, 8) } close(COUNTERREAD); %tally = split(/=|\n/,$buffer); } # if a total for this ID doesn't already exist, make one $tally{$counter_id} = 0 unless (defined($tally{$counter_id})); # the heart of the script :) $tally{$counter_id}++; if ($hidden != 2){ print "\n"; print '' if ($hidden); } # open the counter file for writing open(COUNTERWRITE, ">$counter_file") or (print "Cannot open $counter_file: $!\n
", die "Cannot open $counter_file: $!\n
"); if ($flock){ flock (COUNTERWRITE, 2) } # print the hash back into the counter file foreach my $key (keys %tally){ print COUNTERWRITE "$key=$tally{$key}\n"; } if ($flock){ flock (COUNTERWRITE, 8) } close(COUNTERWRITE); # log this hit. if ($log){ my ($ip, $time, $logname); # if there's a proxy, use 'HTTP_X_FORWARDED_FOR'. Otherwise use REMOTE_ADDR. Hopefully one will work. if ($proxy){ $ip = $ENV{'HTTP_X_FORWARDED_FOR'}; } else{ $ip = $ENV{'REMOTE_ADDR'}; } $time = localtime(time()); $logname = $counter_id . '_log.txt'; open(LOGFILE,">>$logname") or (print "Cannot open $logname: $!\n
", die "Cannot open $logname: $!\n
"); if ($flock){ flock (LOGFILE, 2) } print LOGFILE <<_LOG_; -------------------- Time: $time IP Address: $ip User-Agent: $ENV{'HTTP_USER_AGENT'} _LOG_ if ($flock){ flock (LOGFILE, 8) } close(LOGFILE); } # the end!