Simplifying a php script

Discussion in 'PHP' started by carmen, May 4, 2006.

  1. #1
    I came across this php script on the net as a method of banning IP addresses. It compares the IP address of the surfer to a list of banned ip addesses kept in ip.dat. The script works great.

    What I'm wondering is I maintain 5 separate ban lists (ip.dat, ip1.dat, ip2.dat,ip3.dat,ip4.dat). Rather then replicate the code 5 times to check the surfer ip address against the ip address in each of the 5 lists, is there a way to modify the $IP_array=file('../folder/IP.dat'); line so that it checks all 5 dat files and not just the one.

    Thank you

    <?php
    $IP = $_SERVER['REMOTE_ADDR'];
    $IP_array = file('../folder/IP.dat');
    $IP_array = str_replace("\n", "", $IP_array);
    $IP_array = str_replace("\r", "", $IP_array);
    foreach ($IP_array as $IP_test) {
    if (substr_count($IP, $IP_test) != "0") {

    echo 'Banned World!';

    die();
    }
    }

    echo 'Hello World!';

    ?>
     
    carmen, May 4, 2006 IP
  2. dotcomsdotbiz

    dotcomsdotbiz Banned

    Messages:
    73
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why not combine the .dat files into one file? To me that would be the most straightforward way of getting the result you are looking for.

    Mike
     
    dotcomsdotbiz, May 4, 2006 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Depending on how many IP addresses you intend on banning, this could get pretty slow, pretty quickly.

    If I were to do this, I'd set up a database table myself...
     
    TwistMyArm, May 5, 2006 IP
  4. quaffapint

    quaffapint Active Member

    Messages:
    299
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    If you care about speed at all or get a good number of visits I would definitely place them in a db table with that field indexed for quick searching.
     
    quaffapint, May 5, 2006 IP
  5. carmen

    carmen Peon

    Messages:
    162
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's a very good point that I never thought about. I think down the road, I might migrate to a database approach if this method does seem to slow.

    For the person who was asking, why not consolidate all the ban files into a single one, I could do that, but really the preference is to keep them separate as each file represents a specific category of IP addresses.

    If there is an easy way to modify the original script so that it reads all the input files, without having to replicate the code multiple times, I'd be all ears.

    Thanks
     
    carmen, May 5, 2006 IP
  6. Mystique

    Mystique Well-Known Member

    Messages:
    2,579
    Likes Received:
    94
    Best Answers:
    2
    Trophy Points:
    195
    #6
    I have very good perfomance and banning protection using this script:

    
    <?
    $blocked = array(
    	"127.0.0.2",
             "62.156.152.71",
    	);
    
    for ($i=0; $i<sizeof($blocked); $i++) {
    if ("$blocked[$i]" == "$REMOTE_ADDR") {
    $ip = $blocked[$i];
    $block = 1;
    }
    }
    
    ?>
    
    Code (markup):
    note from the author:

     
    Mystique, May 5, 2006 IP
  7. Edmunds

    Edmunds Peon

    Messages:
    136
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I use the following function to ban IPs

        function check_ip_ban($ipaddress)
        {
            global $options;
    	    $ip_banned = FALSE;
    	    #
    	    # checks if the user's IP address is in IP banned address list
    	    # if, is then, IP bans
    	    #
    	    if (!empty($ipaddress))
    	    {
    		    if (strpos($options['ip_banning'], $ipaddress) !== FALSE)
    		    {
    		        $ip_banned = TRUE;
    		    }
    		    else
    		    {
    		        # full IP isn't banned, checking if parts are
    		        $ip_parts = explode('.', trim($ipaddress));
    		        if (strpos($options['ip_banning'], $ip_parts[0].'.*.*.*') !== FALSE)
    		            $ip_banned = TRUE;
    		        elseif (strpos($options['ip_banning'], $ip_parts[0].'.'.$ip_parts[1].'.*.*') !== FALSE)
    		            $ip_banned = TRUE;
    		        elseif (strpos($options['ip_banning'], $ip_parts[0].'.'.$ip_parts[1].'.'.$ip_parts[2].'.*') !== FALSE)
    		            $ip_banned = TRUE;
    		    } # end if full IP address isn't banned check
    	    }
    
            return $ip_banned;
       	}
    Code (markup):
    where $options['ip_banning'] holds a list of banned addresses like this:
        #		______________
        #      | 223.12.13.13 |
        #      | 223.12.34.*  |
        #      | 223.12.*.*   |
        #      | 223.*.*.*    |
        #      |______________|
        #
    Code (markup):

    As for your question:
    Simply loop the script!

    
    foreach (array(ip.dat, ip1.dat, ip2.dat,ip3.dat,ip4.dat) AS $filename)
    {
        // include script here, except replace IP.dat with $filename
    }
    
    Code (markup):
     
    Edmunds, May 5, 2006 IP
  8. carmen

    carmen Peon

    Messages:
    162
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Edmunds,

    Would you be able to confirm the syntax below. I did make the changes you suggested and received the error message "Parse error: syntax error, unexpected '}' in C:\servers\xampp\htdocs\test.php on line 20"

    <?php

    foreach (array(ip.dat, ip1.dat, ip2.dat,ip3.dat,ip4.dat) AS $filename)

    $IP = $_SERVER['REMOTE_ADDR'];
    $IP_array = file('$filename');
    $IP_array = str_replace("\n", "", $IP_array);
    $IP_array = str_replace("\r", "", $IP_array);
    foreach ($IP_array as $IP_test) {
    if (substr_count($IP, $IP_test) != "0") {

    echo 'Banned World!';

    die();
    }
    }

    echo 'Hello World!';

    }


    ?>
     
    carmen, May 6, 2006 IP
  9. mines

    mines Well-Known Member

    Messages:
    1,127
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #9
    Try putting in some ' ... eg

    foreach (array('ip.dat', 'ip1.dat', 'ip2.dat','ip3.dat','ip4.dat') AS $filename)

    (note: havn't tested this)
     
    mines, May 7, 2006 IP
  10. carmen

    carmen Peon

    Messages:
    162
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I did try that, but there is still something about line 20 that it doesn't like, same error message.
     
    carmen, May 7, 2006 IP
  11. mines

    mines Well-Known Member

    Messages:
    1,127
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    160
    #11
    Also looks like you're missing a { after the for each statement....?
     
    mines, May 7, 2006 IP
  12. Edmunds

    Edmunds Peon

    Messages:
    136
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    <?php
    foreach (array('ip.dat', 'ip1.dat', 'ip2.dat', 'ip3.dat', 'ip4.dat') AS $filename)
    {
    	$IP = $_SERVER['REMOTE_ADDR'];
    	$IP_array = file('$filename');
    	$IP_array = str_replace("\n", "", $IP_array);
    	$IP_array = str_replace("\r", "", $IP_array);
    	foreach ($IP_array as $IP_test)
    	{
    		if (substr_count($IP, $IP_test) != "0")
    		{
    			echo 'Banned World!';
    			die();
    		}
    	}
    
    	echo 'Hello World!';
    
    }
    ?>
    Code (markup):
    Should be correct.
     
    Edmunds, May 8, 2006 IP