如果仅使用IPv4:
- 用于
ip2long()
将IP和子网范围转换为长整数 - 将/ xx转换为子网掩码
- 进行按位“与”(即ip和mask)并检查“结果=子网”
这样的事情应该工作:
function cidr_match($ip, $range){ list ($subnet, $bits) = explode('/', $range); if ($bits === null) { $bits = 32; } $ip = ip2long($ip); $subnet = ip2long($subnet); $mask = -1 << (32 - $bits); $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned return ($ip & $mask) == $subnet;}


