in reply to Testing form params

How about using grep, or even a hash... The grep way:

if (grep { $name eq $_ } @list and $ENV{REMOTE_ADDR} ne "$ip" ) {.....

Or the hash way...

my %hash = qw/name ip name2 ip2 name3 ip3/; if ( exists $hash{$name} and $ENV{REMOTE_ADDR} ne $hash{$name} ) {.... +..


Dave

Replies are listed 'Best First'.
Re: Re: Testing form params
by Anonymous Monk on Jan 13, 2004 at 11:15 UTC
    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);

      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);