You can add a configuration file, and then add some IP addresses that need to be banned to the configuration file through certain rules. When the program is initialized, read each rule in the configuration file, and then use the method provided in this article to check the current Whether the accessed client IP address exists in these rules, if so, the service is denied.
Copy code The code is as follows:
/**
* Check or filter IP addresses in PHP
*
* Support IP range, CIDR (Classless Inter-Domain Routing) and single IP format
* Organized: http://www.CodeBit.cn
* Reference:
* - { @link http://us2.php.net/manual/zh/function.ip2long.php#70055}
* - {@link http://us2.php.net/manual/zh/function.ip2long.php# 82397}
*
* @param string $network network segment, supports IP range, CIDR and single IP format
* @param string $ip IP address to be checked
* @return boolean
*/
function netMatch($network, $ip) {
$network = trim($network);
$ ip = trim($ip);
$result = false;
// IP range : 174.129.0.0 - 174.129.255.255
if (false !== ($pos = strpos($network, "-"))) {
$from = ip2long(trim(substr($network, 0, $pos)));
$to = ip2long(trim(substr($network, $pos+1)));
$ip = ip2long($ip );
$result = ($ip >= $from and $ip <= $to);
// CIDR : 174.129.0.0/16
} else if (false !== strpos($network,"/ ")) {
list ($net, $mask) = explode ('/', $network);
$result = (ip2long($ip) & ~((1 << (32 - $mask)) - 1)) == ip2long($net);
// single IP
} else {
$result = $network === $ip;
}
return $result;
}
// 174.129.0.0 - 174.129 .255.255
var_dump(netMatch(' 174.129.0.0 - 174.129.255.255 ', '174.129.1.31')); // True
var_dump(netMatch(' 174.129.0.0/16 ', '174.139.1.31')); / / False
var_dump(netMatch(' 174.129.1.32 ', '174.129.1.31')); // False
?>
The above introduces the implementation code for checking or filtering IP addresses in the iphone4s caller ID home software PHP, including the content of the iphone4s caller ID home software. I hope it will be helpful to friends who are interested in PHP tutorials.