logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Help with REGEX, Perl, and $ENV{HTTP_REFERER}
Author
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Mon 31 Oct '05 2:31    Post subject: Help with REGEX, Perl, and $ENV{HTTP_REFERER} Reply with quote

I am one, as Steffen can account for, who does not do a alot of coding in Perl. For me, much of Perl is pretty logical and easy enough to follow, but that does not lend itself to understanding the code.

So here is what I need to learn. I want to use a small bit of Perl code, adding to an exisitng file, to check the referer, but I only want to compare "part" of the HTTP_REFERER. Here is the code I am starting with:

Code:
@referers = ('http://www.domain.com', 'http://sub.domain.com');   
$temp = 0;
if ($ENV{'HTTP_REFERER'}) {
   foreach $referer (@referers) {
       if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
          $check_referer = 0;
          last;
       }
   }
} else {
   $temp = 1;
}

if ($temp != 1) {
   # bad referrer
   print 'Bad Referer!!!';
   exit;
}


This code does not work, because there are going to be some GET variables (a query string) being passed along with the URL that is doing the referring.

How can I modify the
if ($ENV{'HTTP_REFERER'} =~ /$referer/i) to check for for this part such as with the PHP function eregi?

e.g. PHP code would be ...
eregi( 'http://www.domain.com', $HTTP_SERVER_VARS['HTTP_REFERER'] )


What would the Perl equivelant be?

Your help is appreciated.

--
Brian
Back to top
Steffen
Moderator


Joined: 15 Oct 2005
Posts: 3092
Location: Hilversum, NL, EU

PostPosted: Mon 31 Oct '05 20:18    Post subject: Reply with quote

You have to use you a regex, the following snippet works here, when the referer starts with http://www.apachelounge.com then the referer is good (without or with a query sring):

Code:

#!perl.exe

print "Content-type: text/html\n\n";
$referer = $ENV{'HTTP_REFERER'};
if ($referer =~ /^http:\/\/www.apachelounge.com/)
{
print "<b>Good Referer !!</b><br> \n";
}
else
{
print "<b>Bad Referer !!</b><br> \n";
}
print "<br>referer is $referer<br><br> \n";
print "<A HREF=\"/ref.pl?file=105&id=6\"> again</A>";



Steffen
Back to top
Brian



Joined: 21 Oct 2005
Posts: 209
Location: Puyallup, WA USA

PostPosted: Mon 31 Oct '05 20:41    Post subject: Reply with quote

Perfect!!!!

Thank you. I found that using this style of code you provided along with mod_rewrite to do exactly what I need, keep people from coming in with out the proper referer.

I really appreicate the help.

--
Brian
Back to top


Reply to topic   Topic: Help with REGEX, Perl, and $ENV{HTTP_REFERER} View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner