Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help make this snippet work better? I have more names to test against (I don't want people trying to post under the admin's name) and it's just too messy doing the test like this. Is there a way to put all the names in @names and if the name exists in there, do this check?
my $ip = ""; # my IP, of course if ($name eq "Administrator" && $ENV{REMOTE_ADDR} ne "$ip" || $name eq + "administrator" && $ENV{REMOTE_ADDR} ne "$ip" || $name eq "Admin" && $ENV{REMOTE_ADDR} ne "$ip" || $name eq "webmaste +r" && $ENV{REMOTE_ADDR} ne "$ip") { print "<font color=red>You are not authorized to post as the Adminis +trator</font>"; exit; }

Replies are listed 'Best First'.
Re: Testing form params
by Zaxo (Archbishop) on Jan 13, 2004 at 09:56 UTC

    If you're doing what this looks like, see Apache's mod_auth*. It will do all the work for you, leaving $ENV{REMOTE_USER} for you to see - already authenticated.

    After Compline,
    Zaxo

Re: Testing form params
by davido (Cardinal) on Jan 13, 2004 at 09:46 UTC
    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

      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
Re: Testing form params
by b10m (Vicar) on Jan 13, 2004 at 09:40 UTC

    Well, others will probably come up with even better snippets, but I'll give it a shot too ;)

    Since, you check for $ip in all cases, why not do it a little like this?

    if($ENV{REMOTE_ADDR} ne $ip) { if($name =~ /^administrator$/i || $name eq "Admin" || $name eq "web +master") { print "<font......"; } }

    HTH

    --
    b10m