You are here: Re: PHP ban script « All PHP « IT news, forums, messages
Re: PHP ban script

Posted by carmen on 05/08/06 09:26

Thank you for your help, this worked great.

You are correct about the database method being faster, but I'm looking
forward to seeing what kind of speed this script will run at as I do want to
apply it to all my websites and if I can avoid having to setup databases, it
will allow faster implementation. If not, I'll go the database route.

Thanks again.

"Shaun" <shaun@mypants.drunkwerks.com> wrote in message
news:flmt5213gdjtsoa0f9g0671cahmr8b70to@4ax.com...
> On Mon, 08 May 2006 05:33:41 GMT, "carmen" <dddd@kkdi.cka> wrote:
>
>>Hello
>>
>>I came across a php script on the net that will compare a web surfers IP
>>address to those in a text file. If a match or partial match is found,
>>the
>>user is banned from accessing the php script, but if no match is found,
>>the
>>php script executes.
>
> For starters, this is something that would be much more efficient if
> it were using a database as opposed to a group of text files. I
> mention this primarily because you do seem to be concerned about
> efficiency.
>
>>The original script is as follows:
>>
>>line 1 <?php
>>line 2 $IP = $_SERVER['REMOTE_ADDR'];
>>line 3 $IP_array = file('../folder/IP.dat');
>>line 4 $IP_array = str_replace("\n", "", $IP_array);
>>line 5 $IP_array = str_replace("\r", "", $IP_array);
>>line 6 foreach ($IP_array as $IP_test) {
>>line 7 if (substr_count($IP, $IP_test) != "0") {
>>line 8
>>line 9 echo 'Banned World!';
>>line 10
>>line 11 die();
>>line 12 }
>>line 13 }
>>line 14
>>line 15 echo 'Hello World!';
>>line 16
>>line 17 ?>
>>
>>I currently have not one text file with banned addresses, but five
>>different
>>one (eg IP.dat, IP1.dat, IP2.dat, IP3.dat, IP4.dat) and was wondering if
>>there was an easy way of changing the code so that it efficiently checks
>>the
>>surfers IP address to all the IPx.dat files to see if it is banned? I
>>could
>>simply replicate the code 5 times to check each file, but if there was a
>>way
>>of just looping through the various files, that would be great.
>
> <?php
> for ($i = 1; $i <= 5; $i++) {
> $ipArray = file('../folder/IP' . $i . '.dat');
> $ipArray = preg_replace("/[\r\n]/", '', $ipArray);
> if (in_array($_SERVER['REMOTE_ADDR'], $ipArray)) {
> die('Banned');
> }
> }
> echo 'Hello, world!';
> ?>
>
>>I know I could merge the 5 IPx.dat files into a single one and not have to
>>worry about this, but for management purposes of the bans, it works a lot
>>better for me having the 5 distinct files.
>
> I would still suggest looking into a database solution if you can,
> especially if you're getting decent traffic. Hitting 5 extra files per
> pageload is not at all efficient compared to sending a query to a
> database that caches things to RAM.
>
> hth
>
> -
> Remove mypants to email.
> <http://www.shaunc.com/>

 

Navigation:

[Reply to this 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

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