Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

if statement based on HTTP_REFERER

by Jesse Smith (Acolyte)
on Apr 09, 2015 at 16:48 UTC ( [id://1122972]=perlquestion: print w/replies, xml ) Need Help??

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

Is it possible to have two different logs generated, and the one that get's data added when the script is loaded depends on if there is a HTTP_REFERER?
if ( THERE IS A $ENV{'HTTP_REFERER'} ) { $shortdate = `date +"%D %T %Z"`; chop ($shortdate); open (DATABASE,">>$database"); print DATABASE "$ENV{'REMOTE_ADDR'} - $shortdate - $ENV{'HTTP_ +USER_AGENT'} - <A HREF=\"http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}\ +">http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}</a> - <A HREF=\"$ENV{'H +TTP_REFERER'}\">$ENV{'HTTP_REFERER'}</a>\n"; close(DATABASE); } else{ $shortdate = `date +"%D %T %Z"`; chop ($shortdate); open (DATABASE2,">>$DATABASE2"); print DATABASE2 "$ENV{'REMOTE_ADDR'} - $shortdate - $ENV{'HTTP +_USER_AGENT'} - <A HREF=\"http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'} +\">http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}</a> - NO HTTP_REFERER\ +n"; close(DATABASE2); }

Replies are listed 'Best First'.
Re: if statement based on HTTP_REFERER
by Perlbotics (Archbishop) on Apr 09, 2015 at 16:56 UTC

    Yes, it's possible.
    You will most probably need some kind of locking (see flock) in order to avoid, that simultaneous invocations do interfere. Have a look at a logging framework like i.e. Log::Log4perl. It will allow you to have diffent loggers, log-rotation, locking etc.

    Update: You also need to check for empty/undefined HTTP_REFERER.

Re: if statement based on HTTP_REFERER
by MidLifeXis (Monsignor) on Apr 09, 2015 at 17:11 UTC

    Be aware (if it matters) that the REFERRER value is set by the client, and is therefore not to be trusted. In other words, it can be spoofed.

    --MidLifeXis

Re: if statement based on HTTP_REFERRER
by Athanasius (Archbishop) on Apr 09, 2015 at 17:18 UTC

    Hello Jesse Smith,

    Another point: Please note that it is both simpler and safer to avoid code duplication whenever possible:

    my $db_name = $DATABASE2; my $suffix = 'NO HTTP_REFERRER'; if (defined $ENV{'HTTP_REFERRER'} && length $ENV{'HTTP_REFERRER'} > 0) { $db_name = $database; $suffix = "<A HREF=\"$ENV{'HTTP_REFERRER'}\">$ENV{'HTTP_REFERRER'} +</a>"; } $shortdate = `date +"%D %T %Z"`; chomp $shortdate; open(my $db, '>>', $db_name) or die "Cannot open database '$db_name' f +or appending: $!"; print $db "$ENV{'REMOTE_ADDR'} - $shortdate - $ENV{'HTTP_USER_AGENT'} +- " . "<A HREF=\"http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}\">htt +p://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}</a> - $suffix\n"; close($db) or die "Cannot close database '$db_name': $!";

    (Code untested, and with suggested improvements included: chomp instead of chop, the 3-argument form of open, a lexical filehandle, and testing for file open and close errors.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1122972]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-24 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found