Reply to Re: PHP IP blocking script is not working...

Your name:

Reply:


Posted by Heiko Richler on 01/24/07 09:44

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация