in reply to Re: Testing form params
in thread Testing form params

Your code works great, thanks man! One last question. Is this how you would do it if you saved a bunch of IP addresses to a text file like:
#banned.txt ##.###.##.### ##.###.##.### ##.###.##.###
And I was trying to test if their $ENV{REMOTE_ADDR} matches any that's in the banned.txt?
my $banned = "banned.txt"; open( BANNED, "$banned" ); $ip = <BANNED>; while (<BANNED>) { if (grep { $ENV{REMOTE_ADDR} eq $_ }) { print "GET AWAY!"; } } close(BANNED);

Replies are listed 'Best First'.
Re: Testing form params
by b10m (Vicar) on Jan 13, 2004 at 11:24 UTC

    No need for grep here :)

    open(BANNED, "<$banned"); while(<BANNED>) { print "GET AWAY!" if ($_ eq $ENV{REMOTE_ADDR}); } close BANNED;
    --
    b10m
      I tried your code..well, I changed a little bit to get it to work, but it won't die and tell me my IP is bad. Did I mess something up? (I checked the banned.txt file and it has my IP with no leading spaces and I saved the IP via print BANNED "$ENV{'REMOTE_ADDR'}\n";
      open(BANNED, "<$banned") or die "Cannot open $banned because: $!"; while(<BANNED>) { if ($_ eq $ENV{REMOTE_ADDR}) { print "<font color=red>Your IP has been banned, you are not allow +ed to use this script.</font>\n"; exit; } } close(BANNED);

        *Doh!*, my bad, $ENV{REMOTE_ADDR} doesn't have a line break, so you might want to chomp the input first:

        open(BANNED, "<$banned") || die "Can't open $banned: $!"; while(<BANNED>) { chomp; if($_ eq $ENV{REMOTE_ADDR}) { print "... you are banned ..."; exit; } } close BANNED;

        update: If you use Apache, you also might want to use it's blocking capabilities :)

        --
        b10m