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
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?
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
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.
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:
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...
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.
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:
Many thanks nico swd... That worked fine for me. Just wish I understood it all. Thanks agian.. John C