I need to check a comma delimited txt file

Discussion in 'PHP' started by jc@ukzone.com, Jan 30, 2007.

  1. #1
    Hi..
    I am almost a complete novice at php scripting.
    I need to check a list of ip numbers from a comma delimited txt file and if it corresponds with "getenv(REMOTE_ADDR)" to disallow a posting.

    I have created a ipfile.txt and read it into a $string.
    I can read the file with print $string;

    RETURNS: 10.0.0.1,10.0.0.2,10.0.0.3,

    My current code in the script is:

    if(
    ($remote_ip == "10.0.0.1")
    || ($remote_ip == "10.0.0.2")
    || ($remote_ip == "10.0.0.3")
    )
    {
    'go away ';
    exit;
    }


    I need to know how to extract the ip number of "getenv(REMOTE_ADDR)" from the ipfile.txt and put it in place of he above code.

    I feel sure that this is rather elementary but I just don't know how to do it (yet).

    Hope you can help.

    John C
     
    jc@ukzone.com, Jan 30, 2007 IP
  2. Dan Nilsson

    Dan Nilsson Peon

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Perhaps if you explode the string returned from the textfile?

    http://se2.php.net/manual/en/function.explode.php

    
    // Example 1
    $pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
    $pieces = explode(" ", $pizza);
    echo $pieces[0]; // piece1
    echo $pieces[1]; // piece2
    
    In your case:
    $ips = explode("," $string);
    
    And compare the $remote_ip:
    if (in_array($remote_ip, $ips)) {
       echo 'go away ';
       exit;
    }
    
    PHP:
    Is this what you are looking for?
     
    Dan Nilsson, Jan 30, 2007 IP
  3. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    First, structure you document so that each IP is on a separate line. Next create some basic functions to do the checking like this:

    
    	function isBannedCheck_Exit(){
    		if ( isBannedIP() === true ){
    			exit('Get out');
    		}
    		return false;
    	}
    	
    	function isBannedIP($ip = NULL){
    		$ipList = file('/path/to/file.txt');
    		array_walk($ipList, 'walk_trim');
    		
    		if ( is_null($ip) ){
    			$ip = $_SERVER['REMOTE_ADDR'];
    		}
    		
    		return in_array($ip, $ipList);
    	}
    	
    	function walk_trim(&$value){
    		$value = trim($value);
    		return;
    	}
    
    PHP:
    Now that you have the functions simply call the isBannedCheck_Exit() function on each page you want the check. If you want every page to include this check then put the code in a common library file.

    Bobby
     
    Chemo, Jan 30, 2007 IP
  4. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There are two more ways to deal with the issue. One is to leave the IP addresses in a file. Then:

    $badIps = file_get_contents("bad-ip-file");
    if( preg_match($remote_ip, $badIps) ) { echo "go away"; exit; }
    Code (markup):
    The second is to put the bad ips into a database and use a SQL query to see if the $remote_ip is in the database.

    It might go something like:

    $results = mysql_query("select ip from bad_ip where ip = $remote_ip");
    if( mysql_num_rows($results) > 0) { echo "go away"; exit; }
    Code (markup):
    The first concept works great as long as the list of banned IPs is not too great. The second idea scales better and as the number of bad IPs rises, it may will give faster results with less impact on memory and CPU cycles.
     
    clancey, Jan 30, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Just on a sidenote, you can use array_map(), so you don't need to create your own trim() function.

    
    
    $ipList = array_map('trim', file('/path/to/file.txt'));
    
    PHP:
     
    nico_swd, Jan 30, 2007 IP
  6. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks to everybody who has replied to my original post.
    I'm not ignorant but I am in the middle of trying to add a Captcha to my form and I am struggling to get it to work.
    I will try out the above suggestions asap but meanwhile I want you all to know that I do appreciate the trouble and help you have given me.
    Later...
     
    jc@ukzone.com, Jan 31, 2007 IP
  7. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks to all who made suggestions to me.
    I have finaly got round to trying this out.

    I decided that CLANCEY's first suggestion looked the simplest so I tried that.
    I got an error message:
    Warning: Delimiter must not be alphanumeric or backslash in /home/countrymusic/countrymusic.org.uk/html/notice-board/add-notice.php on line 94

    This is the script that I put into the php code:
    $badIps = file_get_contents("http://84.45.181.110/bad-ip-file.txt");
    if(preg_match($remote_ip, $badIps)) (this is line 94)
    { echo "go away"; exit; }


    I am using the 'http://84.45.181.110/bad-ip-file.txt' file in some perl scripts, without problems.

    Has anybody got any ideas what is causing this error.
     
    jc@ukzone.com, Jun 19, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Try this instead.
    
    $ips = array_map('trim', file('http://84.45.181.110/bad-ip-file.txt'));
    
    if (in_array($remote_ip, $ips))
    {
        exit('Go away');
    }
    
    PHP:
     
    nico_swd, Jun 19, 2007 IP
  9. jc@ukzone.com

    jc@ukzone.com Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Many thanks nico swd...

    That worked fine for me.

    Just wish I understood it all.

    Thanks agian..

    John C
     
    jc@ukzone.com, Jun 19, 2007 IP