Re: Restrict IP access to a Perl application
- From: Martijn Lievaart <m@xxxxxxxxxxxxxxxx>
- Date: Wed, 30 Jan 2008 08:56:59 +0100
On Tue, 29 Jan 2008 23:04:26 -0800, barramundi9 wrote:
Dear all:
I am a newbie to Perl and have an application written in Perl. I put
IPs that are "allowed" to access the application into a file called
"ip.allow".
I then tried to compare the $ENV{REMOTE_ADDRESS} to the IPs in
"ip.allow" to determine the access right which looks like the following:
10.0.0.1
10.0.0.2
10.0.0.3
And the code is:
$address=$ENV{'REMOTE_ADDR'};
open(FILE,"/path/to/ip.allow") or die ("Cannot open file!");
flock(FILE,2);
while ($line=<FILE>) {
$line=~s/\./\\\./g;
if ($line =~ /$address/) {
print "IP matched!!\n";
last;
}
}
flock(FILE,8);
close(FILE);
But it doesn't seem to work because when I take out 10.0.0.1 from the
ip.allow file, 10.0.0.1 can still access the application.
1) You don't chomp the input line, so it still contains a \n
2) You can just compare strings, no need for the regexp
3) You forgot to anchor your regexp (/^$address$/), but see 2)
HTH,
M4
.
- References:
- Restrict IP access to a Perl application
- From: barramundi9
- Restrict IP access to a Perl application
- Prev by Date: Re: Restrict IP access to a Perl application
- Next by Date: FAQ 9.18 How do I decode a MIME/BASE64 string?
- Previous by thread: Re: Restrict IP access to a Perl application
- Next by thread: Re: Restrict IP access to a Perl application
- Index(es):
Relevant Pages
|