#! /usr/local/bin/perl -Tw # whatbrowser.pl - from the Complete Webmaster # http://abiglime.com/webmaster # written by Doug Steinwand # # Keeps a log and displays a bar chart based on the # browser that is currently being used. To be used as # a server-side include program, not a CGI script. # # path and name for the access log $logfile="/tmp/access.txt"; # colors $boxcolor="#002288"; $txtcolor="#ffffff"; $barcolor="#ff0000"; # font size $fontsize="2"; # box width $boxwidth="50%"; # Examples of User Agent Strings: # # Mozilla/4.04 [en] (X11; U; Linux 2.0.30 i586) # Mozilla/3.0 WebTV/1.2 (compatible; MSIE 2.0) # Mozilla/2.0 (compatible; MSIE 3.0; AOL 3.0; Windows 3.1) # Mozilla/2.0 (compatible; MSIE 3.0B; Win32) # Mozilla/4.01 (Macintosh; I; PPC) # Determine browser from the HTTP_USER_AGENT environment variable. # note: Netscape's numbers may be inflated because many non- # Netscape broswers claim to be "Mozilla" compatible $agent=$ENV{HTTP_USER_AGENT}; if ($agent=~/WebTV/i) { $agent="WebTV"; } elsif ($agent=~/MSIE/i) { $agent="MSIE"; } elsif ($agent=~/Mozilla/i) { $agent="Netscape"; } else { $agent="Other"; } # open the log file for read/write access unless (open FH, "+<$logfile" or open FH, ">$logfile"){ print "Can't open $logfile: $!\n"; exit; } # exclusive lock flock FH, 2; # read the file into %log while() { chomp; ($k,$v,$i)=split(/\s+/); $log{$k}=int($v); $ip{$k}=$i; } # increase the count for this agent, # only if it came from a different IP address if ($ip{$agent} ne $ENV{REMOTE_ADDR}) { $log{$agent}++; $ip{$agent}=$ENV{REMOTE_ADDR}; # reset to write at the start of the file seek FH, 0, 0; # write back each key and value foreach (sort keys %log) { print FH $_," ",$log{$_}," ",$ip{$_},"\n"; } # erase remainder of file, if any truncate FH, tell(FH); } close FH; # find the sum $total=0; foreach (values %log) { $total+=$_; } # avoid divide by zero error $total=1 if $total==0; print < What Browses Here EOT foreach (sort keys %log) { $p=sprintf("%.0f",($log{$_}*100/$total)); $n=100-$p; # draw a bar print < EOT } print "
$_ $p%
   
\n";