Forum Moderators: coopster & phranque

Message Too Old, No Replies

Do not ban Ip from admins

Do not ban the ip from admins

         

StopSpam

1:38 pm on Jun 8, 2003 (gmt 0)

10+ Year Member



i got a guestbook script (Perl) that ban visitors for flooding or double posting.
and word fillter ect

but iwant admins to be allow flooding and bad words

so i try add this code:

$adminip="48.70.9.28";
if ($visitors == $adminip) { exit; }

so it says to script if visitor ip is an admin ip stop dont ban
and somehow works ...(but only with one ip is listed)

Now i need to add 3 admin ips how can i do this?

i try this code but wont work ( it only reads and not ban the first ip)
$adminip="48.70.9.28","11.23.44.55","24.67.18.8";
if ($visitors == $adminip) { exit; }

i also try this . But wont work at all:
@adminip="123.44.33.55","11.23.44.55","24.67.18.8";
if ($visitors == @adminip) { exit; }

Can some one explain/show me how to do this
please advise thanks ;-)

[edited by: StopSpam at 3:15 pm (utc) on June 8, 2003]

Birdman

1:52 pm on Jun 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

$adminip=array("48.70.9.28","11.23.44.55","24.67.18.8");

if (!in_array($visitors,$adminip)){
Your ban routine here.
}

That says if the the ip is not in the array, run the ban routine.

daisho

2:34 pm on Jun 8, 2003 (gmt 0)

10+ Year Member



Or more to you first example:

if(in_array($visitors,$adminip)){
exit;
}

If the IP is in the array then exit.

daisho.

StopSpam

3:51 pm on Jun 8, 2003 (gmt 0)

10+ Year Member



Birdman and daisho thanks for the reply ...
i forgot to mention oen iportant thing
and that is the script is written in Perl not php ...

the code you suguested wiol probally work in php but not on php

i hope someone canpost a perl solution for me ;-)

thanks any way ...
i thin i go learn php as well

ggrot

4:55 pm on Jun 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the very least, you could write 3 if statements:
$adminip="48.70.9.28";
if ($visitors == $adminip) { exit; }
$adminip="1.2.3.4";
if ($visitors == $adminip) { exit; }
$adminip="5.6.7.8";
if ($visitors == $adminip) { exit; }

StopSpam

8:27 pm on Jun 8, 2003 (gmt 0)

10+ Year Member



$adminip="48.70.9.28";
if ($visitors == $adminip) { exit; }
$adminip="1.2.3.4";
if ($visitors == $adminip) { exit; }
$adminip="5.6.7.8";
if ($visitors == $adminip) { exit; }

if iuse one it works
some how more dont work ..
evenif i try like this;

$visitors = $ENV{'REMOTE_ADDR'};
$adminip2="1.2.3.4";
if ($visitors == $adminip2) { exit; }

$visitors = $ENV{'REMOTE_ADDR'};
$adminip3="5.6.7.8";
if ($visitors == $adminip3) { exit; }

$visitors = $ENV{'REMOTE_ADDR'};
$adminip4="7.6.7.8";
if ($visitors == $adminip4) { exit; }

$visitors = $ENV{'REMOTE_ADDR'};
$adminip5="8.6.r.28";
if ($visitors == $adminip5) { exit; }

HTTP_Daemon

4:10 am on Jun 9, 2003 (gmt 0)

10+ Year Member



Firstly, although using "==" for comparison works in this case, it's inadvisable. That's the numeric comparison operator; you're comparing the IP addresses as strings so you want to use 'eq' instead. Indeed, if you 'use strict' or warnings Perl complains: "Argument "48.70.9.29" isn't numeric in numeric eq (==) at foo.pl line 5".

Anyway, I'd probably solve the problem like this:

my @admin_ips = qw(48.70.9.28 48.70.9.29);
my $visitor_ip = "48.70.9.28";
exit if grep(($visitor_ip eq $_),@admin_ips);

The admin's IPs are held in an array, and the visitor's IP is held in a scalar. The grep function evaluates each element of the @admin_ips array, setting $_ to the value of each element in turn. If $visitor_ip is ever equal to $_, i.e. if it is an element of @admin_ips, the expression returns true; causing the script to exit.

Or:

my @admin_ips = qw(48.70.9.28 48.70.9.29);
my $visitor_ip = "48.70.9.28";
for (@admin_ips) { exit if $visitor_ip eq $_ };

If you had a far larger set of data, you'd be better off doing something like this:

my @admin_ips = qw(48.70.9.28 48.70.9.29);
my $visitor_ip = "48.70.9.28";
my %admin_ips = map { $_ => 1 } @admin_ips;
exit if $admin_ips{$visitor_ip};

Here, the map function iterates through the @admin_ips array, aliases $_ to each element it encounters, the evaluates the expression "$_ => 1", and returns the result. That's helpful because the lvalue is a hash, which expects two values in a key-value form. This results in a hash, %admin_ips, being created with the administrator IP addresses as the keys and '1' as the value. The last line causes the script to exit if $visitor_ip is a key of the hash.

StopSpam

9:04 am on Jun 9, 2003 (gmt 0)

10+ Year Member



Hi thanks for the detailed answer learned a lot form it ...

inow use this code;

#######
my @admin_ips = qw(48.70.9.28 4 20.40.81.28 43.52.42.12);
my $visitor_ip = "$ENV{'REMOTE_ADDR'}";
exit if grep(($visitor_ip eq $_),@admin_ips);
#######

only problem is it returns a 501 but it wont ban admins
is there way not toget 501? i think the script exits or ends to rute?

StopSpam

1:29 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



i solved the 501 error ending

i just added these above the do npot ban admin code;

print "Content-type: text/html\n\n";
print " Admin a visitor would have gotten banned by now \n";

HTTP_Daemon

3:28 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



I had the script exit when it determined the user was an administrator because I thought that was what you wanted. ;)

I also assumed that snippet would be included within a larger script that would have already printed the correct Content-type, etc. As this is a CGI script, you'd probably want to end it with a message informing the administrators what happened and closing the, presumably, opened <body> and <html> tags. If so, you could define a subroutine that printed that message, closed the tags, and then exited the script. You would replace the original call to _exit_ with a call to this subroutine.

StopSpam

4:07 pm on Jun 10, 2003 (gmt 0)

10+ Year Member



Hi thx ... Your more then right your code is corect
i just had it placed toearly in the script and there for gave error now it ahows the same message a visitor is getting but now admin aint banned ....

so your saying if ichange :
exit if grep(($visitor_ip eq $_),@admin_ips);

into something like:

my @admin_ips = qw(48.70.9.28 4 20.40.81.28
$close if grep(($visitor_ip eq $_),@admin_ips);

or

&close if grep(($visitor_ip eq $_),@admin_ips);

i can have it print a $ or run a sub &?

is that correct?