By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

PHP IP blocking script is not working...

P: n/a
Hello,

I wonder if anybody can help. I have an IP blocking script which
displays a blank screen if an IP is detected from a list in an external
file. The problem is, the script only reads the last IP¨in the file, not
every one. Can somebody tell me where this is going wrong?

<?php

//get user's IP address
$userip = $_SERVER['REMOTE_ADDR'];

//get list of banned IPs from external file
$filename="http://www.mysite.com/banned.txt";
$bannedlist = array();
$file = fopen($filename, "r");
while(!feof($file)) {

//read file line by line into a new array element
$bannedlist[] = fgets($file, 4096);
}
fclose ($file);

//check if the current user's IP is in the banned list
if (in_array($userip, $bannedlist))
{
//it is, so send him packing
echo "This website is currently unavailable";
exit();

}

//continue executing the script

?>

The banned.txt file is simple a list of IPs, one per line, as follows:

82.207.17.160
84.235.100.2
87.118.106.177
193.69.180.120
84.244.7.168

I update it using Dreamweaver and upload to my site.

If anybody could help with this I would be grateful. Thank you

Françoise
Jan 24 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Françoise Debat schreef:
Hello,

I wonder if anybody can help. I have an IP blocking script which
displays a blank screen if an IP is detected from a list in an external
file. The problem is, the script only reads the last IP¨in the file, not
every one. Can somebody tell me where this is going wrong?
<snip long script>

$banned_list = file($file)
if (in_array($_SERVER['REMOTE_ADDR'], $bannedlist))
{
echo "This website is currently unavailable";
exit();
}
--
Arjen
http://www.hondenpage.com
Jan 24 '07 #2

P: n/a
Françoise Debat wrote:
<?php

//get user's IP address
$userip = $_SERVER['REMOTE_ADDR'];

//get list of banned IPs from external file
$filename="http://www.mysite.com/banned.txt";
$bannedlist = array();
$file = fopen($filename, "r");
while(!feof($file)) {

//read file line by line into a new array element
$bannedlist[] = fgets($file, 4096);
foef is true after the last fgets fails!
}
fclose ($file);
what does print_r($bannedlist); tell you?
//check if the current user's IP is in the banned list
if (in_array($userip, $bannedlist))
{
//it is, so send him packing
echo "This website is currently unavailable";
exit();

}

//continue executing the script

?>
Do not copy the file in an array. Check row by row. Forget about feof.

I would do this:

$file = fopen($filename, "r");
while($row = fgets($file, 4096)) {
//read file line by line
if ($userip == trim($row)) {
//it is, so send him packing
echo "This website is currently unavailable";
exit();
}
}
fclose ($file);
Heiko
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
Jan 24 '07 #3

P: n/a
PS:
Heiko Richler wrote:
while($row = fgets($file, 4096)) {
fgets returns FALSE if an error occurs, so this may be better:

while(is_string($row = fgets($file, 4096)))

or

while(($row = fgets($file, 4096)) !== false)
//read file line by line
Heiko
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
Jan 24 '07 #4

P: n/a
"Arjen" <do**@mail.mewrote in message
news:45***********************@news.wanadoo.nl...
Françoise Debat schreef:
>Hello,

I wonder if anybody can help. I have an IP blocking script which displays
a blank screen if an IP is detected from a list in an external file. The
problem is, the script only reads the last IP¨in the file, not every one.
Can somebody tell me where this is going wrong?

<snip long script>

$banned_list = file($file)
if (in_array($_SERVER['REMOTE_ADDR'], $bannedlist))
{
echo "This website is currently unavailable";
exit();
}
That's just one line if you *really* want to optimize (^__^)

in_array($_SERVER['REMOTE_ADDR'], file($file))
and exit("This website is currently unavailable");

(Kid's, don't try this at home or at all. Writing really short code can be
quite confusing and very unmaintainable...)
Jan 24 '07 #5

P: n/a
Kimmo Laine wrote:
"Arjen" <do**@mail.mewrote in message
news:45***********************@news.wanadoo.nl...
>Françoise Debat schreef:
>>Hello,

I wonder if anybody can help. I have an IP blocking script which
displays a blank screen if an IP is detected from a list in an external
file. The problem is, the script only reads the last IP¨in the file, not
every one. Can somebody tell me where this is going wrong?

<snip long script>

$banned_list = file($file)
if (in_array($_SERVER['REMOTE_ADDR'], $bannedlist))
{
echo "This website is currently unavailable";
exit();
}

That's just one line if you *really* want to optimize (^__^)

in_array($_SERVER['REMOTE_ADDR'], file($file))
and exit("This website is currently unavailable");

(Kid's, don't try this at home or at all. Writing really short code can be
quite confusing and very unmaintainable...)
Kimmo, you MUST love Perl. :P

Regards,
Erwin Moller
Jan 24 '07 #6

P: n/a
Arjen wrote:
Françoise Debat schreef:
>Hello,

I wonder if anybody can help. I have an IP blocking script which
displays a blank screen if an IP is detected from a list in an
external file. The problem is, the script only reads the last IP¨in
the file, not every one. Can somebody tell me where this is going wrong?

<snip long script>

$banned_list = file($file)
if (in_array($_SERVER['REMOTE_ADDR'], $bannedlist))
{
echo "This website is currently unavailable";
exit();
}

I'm sorry, I don't understand why, but this solution (none in this
thread) solved the problem.

Thanks for the help anyway though :)

Françoise
Jan 24 '07 #7

P: n/a
Heiko Richler wrote:
PS:
Heiko Richler wrote:
>while($row = fgets($file, 4096)) {

fgets returns FALSE if an error occurs, so this may be better:

while(is_string($row = fgets($file, 4096)))

or

while(($row = fgets($file, 4096)) !== false)
> //read file line by line

Heiko
Thank you very much! This works beautifully! :))

Kind regards

Françoise
Jan 24 '07 #8

P: n/a
"Erwin Moller"
<si******************************************@spam yourself.comwrote in
message news:45*********************@news.xs4all.nl...
Kimmo Laine wrote:
>"Arjen" <do**@mail.mewrote in message
news:45***********************@news.wanadoo.nl. ..
>>Françoise Debat schreef:
Hello,

I wonder if anybody can help. I have an IP blocking script which
displays a blank screen if an IP is detected from a list in an external
file. The problem is, the script only reads the last IP¨in the file,
not
every one. Can somebody tell me where this is going wrong?

<snip long script>

$banned_list = file($file)
if (in_array($_SERVER['REMOTE_ADDR'], $bannedlist))
{
echo "This website is currently unavailable";
exit();
}

That's just one line if you *really* want to optimize (^__^)

in_array($_SERVER['REMOTE_ADDR'], file($file))
and exit("This website is currently unavailable");

(Kid's, don't try this at home or at all. Writing really short code can
be
quite confusing and very unmaintainable...)

Kimmo, you MUST love Perl. :P
You'd think that, but actually no. I've never learned perl - I haven't even
tried it. :)

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 24 '07 #9

P: n/a
Françoise Debat wrote:
Hello,

I wonder if anybody can help. I have an IP blocking script which
displays a blank screen if an IP is detected from a list in an external
file. The problem is, the script only reads the last IP¨in the file, not
every one. Can somebody tell me where this is going wrong?
Saw you already had it working, but just thought I'd point out that
since you're manually adding the IP's, you could also have done this
with .htaccess

Not sure if it's any more efficient, just another method. The
overhead's got to come from *somewhere*, either the web server parsing
the .htaccess file PHP reading your script to check for banned IP's.
Jan 24 '07 #10

P: n/a
<comp.lang.php>
<Françoise Debat>
<Wed, 24 Jan 2007 10:30:10 +0100>
<45***********************@news.orange.fr>
The banned.txt file is simple a list of IPs, one per line, as follows:

82.207.17.160
84.235.100.2
87.118.106.177
193.69.180.120
84.244.7.168
Theres no need to search a whole list .

Convert e.g. 127.0.0.1 to 127_0_0_1

$poop=str_replace(".","_",$poop);
www.yourdomain.com/banned/127_0_0_1
- grab the live ip address as e.g. $demo
$pass=1;

$filename="banned/$demo";

if (!file_exists($filename)) {$pass=0;}

if ($pass==0) {exit();}
In effect , You could have 10,000 banned ip addresses and the above
method would only do a single look up search instead of searching a
10,000 list of ip addresses .
--
www.phptakeaway.co.uk
(work in progress)
Jan 25 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.