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

I don't have access to my ISP's server logs but want to have some idea of who's visiting my website, this script mail visitors info.
I want excluse my own IP, so I add one line, $ips (IP addresses I want to ignore), for my IP:
unless ($ENV{REMOTE_ADDR} =~ /$ips/) {
..... }
Also I add use strict; use warning;
Is the changes I have make correct?
#!/usr/bin/perl use strict; use warning; $sendmail_path='/usr/lib/sendmail'; $to_address='my@domain.com'; $ips = '127.0.0.1'; #DONT CHANGE ANYTHING BELOW THIS LINE unless ($ENV{REMOTE_ADDR} =~ /$ips/) { 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 "\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"; }

Edited by Arunbear: Changed title from 'Adjusting one feature', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Code Review of Visitor Logging Script
by eibwen (Friar) on May 07, 2005 at 17:59 UTC

    Please reply to -- or at the very least, mention -- the original threads: please help to exclude my own IP address. and adding save logfile feature.

    This version of the code actually features more problems than the original. For example:

    • several lines contain /^+/, indicating that you copied the code from the original post with code-wrapping turned on
    • Warnings are enabled with use warnings; (note the plural)

    While I concur with perleager (if it works, it works -- at least pending correcting syntax errors, et all), I must also point out that I think the responses you received to Re^2: adding save logfile feature offer a considerably better solution.

      Well, I still not tried modified code, so I dont know will it work as expected. However, I try to find better solution for.
Re: Code Review of Visitor Logging Script
by ambrus (Abbot) on May 07, 2005 at 18:05 UTC

    Well, $ips should have anchors so that it would not match part of an ip adress. It's not likely to make any difference in this case, but for example the regexp /66.39.54.27/ will match 166.39.52.27 too which may not be what you want.

    Also, use strict "vars" is not likely to work with this code.

Re: Code Review of Visitor Logging Script
by perleager (Pilgrim) on May 07, 2005 at 17:10 UTC
    Hello

    If your code works to exclude your visits when your view your page through your local machine, then I guess there is nothing really much to add.

    A suggestion: Test to see if the Remote_host address matches the Remote_addr, then decide to exclude it.

    if ($ENV{'REMOTE_HOST'}) { $host=$ENV{'REMOTE_ADDR'}; } else { $host=$ENV{'REMOTE_HOST'}; } unless ($host eq "12.0.0.7") { &send_info_in_mail; }

    perleager
    A reply falls below the community's threshold of quality. You may see it by logging in.