http://www.perlmonks.org?node_id=453351

amalgama has asked for the wisdom of the Perl Monks concerning the following question:

Please, help me adjust a little statictic counter script to exclude my own IP address.
I am finding my stats a bit inflated because working on my own site generates a lot of hits from my home address.
Though this code has a terrible note not recommending to change something inside code itself, I'll risk ask help from valuable monks..
#!/usr/bin/perl $sendmail_path='/usr/lib/sendmail'; $to_address='email@domain.com'; #DONT CHANGE ANYTHING BELOW THIS LINE if (length($ENV{HTTP_X_FORWARDED_FOR})>1) { $xf=$ENV{HTTP_X_FORWARDED_ +FOR}; } else { $xf='NULL'; } $ra=$ENV{REMOTE_ADDR}; @numbs=split(/\./, $ra); $address=pack("C4", @numbs); $gothostbyaddr=gethostbyaddr($address, 2) +; $gt=gmtime; $head=$ENV{REMOTE_ADDR}; @tl=(85,78,73,46,68,69); ($gothostbyaddr) && ($head=$gothostbyaddr); if (length($ENV{QUERY_STRING})>1) { $reff=$ENV{QUERY_STRING}; } else {$reff=$ENV{HTTP_REFERER}; $reff=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; } $head.=" $reff"; $from_address="a".$to_address; open(MH,"|$sendmail_path -t"); print MH "To: $to_address\nFrom: $from_address"; print MH "\n"; print MH "Subject: Visitor Info $head\n\nHTTP_REFERER $reff"; print MH "\n"; print MH "DNS $gothostbyaddr\nIP $ENV{REMOTE_ADDR}"; print MH "\n"; print MH "BROWSER $ENV{HTTP_USER_AGENT}\nWHEN $gt GMT"; print MH "\n"; print MH "FORWARDED_FOR $xf"; print MH "\n"; print MH "APPS $ENV{HTTP_ACCEPT}\n\n"; print MH "MVI CGI Script 1.3 http://mviscript.hypermart.net"; print "\n"; close MH; print "Content-type: image/x-xbitmap\n\n"; print "#define name_width 1"; print "\n"; print "#define name_height 1"; print "\n"; print "static char name_bits[] = { 0x04 };"; print "\n";

Replies are listed 'Best First'.
Re: please help to exclude my own IP address.
by fokat (Deacon) on May 02, 2005 at 18:18 UTC

    NetAddr::IP gives you a convenient way to do what you want.

    Say you know your home IP address can be in the range 10.0.0.0 to 10.0.0.255 (or 10.0.0/24, in proper CIDR notation). Your code could then...

    use NetAddr::IP; my $home_net = new NetAddr::IP '10.0.0/24'; # ... while (<HANDLE_READING_YOUR_LOG>) { my ($string_ip, $rest) = split(/\s+/, $_, 2); my $ip = new NetAddr::IP $string_ip; next if $home_net->contains($ip); # Happily process $string_ip and $rest as it is not from your home n +et # ... }

    Best regards

    -lem, but some call me fokat

Re: please help to exclude my own IP address.
by gman (Friar) on May 02, 2005 at 18:16 UTC
    how about wrapping it in an if statment.

    if($ENV{REMOTE_ADDR} =~ '10.10.0.1') { your code }
      Sorry that should be
      if($ENV{REMOTE_ADDR} ne '10.10.0.1') { your code }
        You could also flip the logic and say something like:

        exit if ($ENV{REMOTE_ADDR} =~ '10.10.0.1');
        where exactly should goes this line?
Re: please help to exclude my own IP address.
by TedPride (Priest) on May 03, 2005 at 03:12 UTC
    To be honest, I don't see how this is a big concern. If you're getting statistics based on views, you may as well include your own views in the count - nobody else goes to the bother of removing them. If you're getting statistics based on visits (unique IPs or IP masks), then your visits won't make up a significant percentage of the total, and you may as well include them too.

    Best thing to do is just cache the last x number of IPs in a text file and check the current IP against them. People who visit the site several times close together (like you) will only be counted once. If you want, you can also store timestamps, so people who visit your site again after x number of days will be counted even if they're still in the IP cache.

      yes, its looks not bad idea to check the current IP against cached IPs. This will required to have on server an additional text file with IP list? Will this slow down script? What if use cookies?