PHP:如何检测IPv6在IPV6范围内? [英] PHP: How to detect IPv6 is on an IPV6 Range?

查看:382
本文介绍了PHP:如何检测IPv6在IPV6范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ipv6范围,但是我不知道如何检测$ _SERVER ['REMOTE_ADDRESS']是否在ipv6范围内?需要帮忙.谢谢

I have an ipv6 range, but I have no clue how to detect if the $_SERVER['REMOTE_ADDRESS'] is in the ipv6 range? 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
}

这篇关于PHP:如何检测IPv6在IPV6范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆