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

I have a statistic log script(called via image tags) it send logs on email. All is fine, the only one thing is I want add save log file feature, in addition to create txt log files on server. Don't know how to implement this feature correctly, where and waht change, so hope for some help...
I know, that it should be added path
$logfile = 'http://www.domain/logfile.txt';
Next goes something that still difficult for me, like
#print "Content-type: text/plain\n\n";
#!/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: adding save logfile feature
by Joost (Canon) on May 04, 2005 at 18:41 UTC
    There is so much stuff wrong with that code I strongly suggest you drop it in favor of something else. Like looking at the server's access log, or using a real log analyzer.

    Your hosting provider probably already has a statistics package installed, or otherwise use one of the free services that are available. I know some people are quite fond of nedstat, but that's just a suggestion, not professional advice :-)

      I dont know what bad is in that simple script. I use it, and its work, it send real stats by email. Sure, it can be improved(how?)I really hate use hosting provider ststistic due it extremelly inconvenience. Taking the statistic from webhost ISp it is impossible for me. even if will it possible, again, it slow, ugly process, requred logging into account, etc, etc.
        That script doesn't calculate any statistics, it just sends an email for every visitor. Even really simple statistics packages show the information in a much better format. As soon as you get more than 10 visits a day, you'll get bored with your approach - even if you dump all of those reports to file.

        As for logging in - I don't have to for the stats page on my site, and as far as I can see, neither do you need to log in for the nedstat reports. I would actually prefer opening a webpage (and log in, if I had to) once every other day to look at a nicely formatted report over going through 10 - 1000 email messages each day.

        As for the problems with the script - it's so horribly written that I'd rather not look at it again, but I wouldn't be surprised if someone with a little time on their hands could use it to send spam and/or break into your site.

        update: sorry if I seem a little rude, reading ugly code makes makes me angry. :-) I stand by my suggestions by the way. Please consider the problem you want to solve, then try to find a good solution. Don't keep hacking on a bad idea.