简体   繁体   English

PHP:如何检测IPv6在IPV6范围内?

[英]PHP: How to detect IPv6 is on an IPV6 Range?

I have an ipv6 range, but I have no clue how to detect if the $_SERVER['REMOTE_ADDRESS'] is in the ipv6 range? 我有一个ipv6范围,但是我不知道如何检测$ _SERVER ['REMOTE_ADDRESS']是否在ipv6范围内? Need help. 需要帮忙。 Thanks 谢谢

The easiest way to check if an address is in a range is to convert the address and the limits of the range to binary and then use normal compare operators: 检查地址是否在范围内的最简单方法是将地址和范围的限制转换为二进制,然后使用常规比较运算符:

$first_in_range = inet_pton('2001:db8::');
$last_in_range = inet_pton('2001:db8::ffff:ffff:ffff:ffff');

$address = inet_pton($_SERVER['REMOTE_ADDR']);

if ((strlen($address) == strlen($first_in_range))
&&  ($address >= $first_in_range && $address <= $last_in_range)) {
    // In range
} else {
    // Not in range
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM