read file with ip range and see if supplied IP matches any range in file
<p>I am trying to read a file with ip/mask ranges and if the supplied IP matches any range in the file it will return with TRUE or similar function. Here is the code I have below</p>

<pre><code>function myip2long($ip) {  
   if (is_numeric($ip)) {  
       return sprintf("%u", floatval($ip));  
   } else {  
       return sprintf("%u", floatval(ip2long($ip)));  
   }  
}

function ipfilter($ip) {  
   $match = 0;  

   $ip_addr = decbin(myip2long($ip));  

   if (file_get_contents('./countryip/all-zones/us.zone')) {  
       $source = file('./countryip/all-zones/us.zone');  

       foreach ($source as $line) {  

           $network = explode("/", $line);  
           $net_addr = decbin(myip2long($network[0]));  
           $cidr = $network[1];  

           if (substr($net_addr, 0, $cidr) == substr($ip_addr, 0, $cidr)) {  
               $match = 1;  
               break;  
           }  
       }  
   }  
   return $match;  
} 

$user_ip = $_SERVER['REMOTE_ADDR'];  

if (ipfilter($user_ip) == 1)  echo "&lt;br /&gt;allowed! Your IP is a United States IP!";  
else echo "deny!";
</code></pre>

<p>An example file (like the one in the example above) is available here
<a href="http://www.ipdeny.com/ipblocks/data/countries/us.zone" rel="nofollow">http://www.ipdeny.com/ipblocks/data/countries/us.zone</a></p>

<p>Not sure if the code above is correct, I got it from here'
<a href="http://www.php.net/manual/en/function.ip2long.php#86793" rel="nofollow">http://www.php.net/manual/en/function.ip2long.php#86793</a></p>
