|
Posted by Shaun on 05/08/06 08:46
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]
|