You can add a configuration file, then add some IP addresses that need to be banned through a set of rules to the configuration file, read each rule in the configuration file when the program is initialized, and then check to see if the current access client IP address exists in these rules, if one exists, by using the method provided in this article. Refuses to provide the service.
Copy Code code as follows:
<?php
/**
* Check or filter IP address in PHP
*
* Support IP interval, CIDR (classless Inter-Domain Routing) and single IP format
* Finishing: 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, support IP interval, CIDR and single IP format
* @param string $IP The IP address to check
* @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
?>
Because most of the Chinese are dynamic IP addresses, restricting access through IP addresses has some limitations and requires caution when used, but is also useful for emergency restricted access.