You can add a configuration file by adding a certain number of IP addresses that need to be banned to the configuration file, read each rule in the configuration file at the time of program initialization, and then use the method provided in this article to check that the client IP address of the current access exists in these rules, if any, Service is denied.
Copy CodeThe code is as follows:
/**
* Check or filter IP address in PHP
*
* Support IP range, 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 range, 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
?>
Due to the fact that most of China uses dynamic IP addresses, restricting access through IP addresses has some limitations, and it needs to be cautious when used, but it is also useful for emergency restricted access.
http://www.bkjia.com/PHPjc/324737.html www.bkjia.com true http://www.bkjia.com/PHPjc/324737.html techarticle You can add a configuration file to the configuration file by adding some IP addresses that need to be banned, and read each of the config files in the configuration file when the program is initialized.