Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
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.
| |
|
Topic: Help with REGEX, Perl, and $ENV{HTTP_REFERER} |
|
Author |
|
Brian
Joined: 21 Oct 2005 Posts: 209 Location: Puyallup, WA USA
|
Posted: Mon 31 Oct '05 2:31 Post subject: Help with REGEX, Perl, and $ENV{HTTP_REFERER} |
|
|
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: 3094 Location: Hilversum, NL, EU
|
Posted: Mon 31 Oct '05 20:18 Post subject: |
|
|
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
|
Posted: Mon 31 Oct '05 20:41 Post subject: |
|
|
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 |
|
|
|
|
|
|