473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

PHP IP blocking script is not working...

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
10 2192
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
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
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
"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
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
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
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
"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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tanel | last post by:
Hello, I need to read a result of the first script (that takes some time to run) from the second script so that the first script doesn't block the second. Here is a short example. do_smth_else()...
2
by: Kasper Ovi | last post by:
Hi After having some problems installing NAV2004, my javascript support stopped working. Currently I've unisntalled all antivirus programs on my PC (as it wont install properly). I'm an...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
4
by: Richard | last post by:
hi, I am using SQL SERVER 2000. Problem that i am facing is when ever I check locks in Enterprise Manager I find following blocking - : 1) sp_cursoropen;1 2) sp_cursorclose;1 3)...
23
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I...
38
by: Emmett | last post by:
I have a simple jacascript that randomizes the background of the top frame of my webpage http://www.duke.edu/~efn. For some reason, my Internet Explorer started blocking the background and...
7
by: Michi Henning | last post by:
Hi, I'm using a non-blocking connect to connect to a server. Works fine -- the server gets and accepts the connection. However, once the connection is established, I cannot retrieve either the...
5
by: Simon Knox | last post by:
Hi I have a web app that has a legitimate use for pop up windows. My web app is an insurance quoting app. I use the window.open method to display another aspx page so that the user can check...
3
by: Bob | last post by:
Hi, I have an app that has a 3rd party phone answering control (4 of ) (interfacing with dialogic 4 line card) attached to the main form. each control raises an event when its Dialogic line...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.