php判断ipv6是否在范围内,[PHP] IPv6檢查IP是否在某個網段內 mtachcidr6

自動目錄

由於 IPv4 可以轉成十進位用推移的運算,但是 IPv6 要轉成十進位的話,會超過有效位數的顯示,另外由於ipv6本身是用十六進位表示,沒必要換成10進位。所以要用更漂亮的作法。我把他寫成 class  = =!!:

函式

/**

* ipv6 class by axer@ms1.boe.tcc.edu.tw

*/

class ipv6{

/**

* ExpandIPv6Notation2Bin()- Convert an ipv6 address to bin string

* @param string $ip6 - an ipv6 address

* @return return the binary string of an ipv6 address if parameter ip6 is an ipv6 address,

* else it return an empty string.

*/

public function ExpandIPv6Notation2Bin($ip6) {

if (strpos($ip6, '::') !== false)

$ip6 = str_replace('::', str_repeat(':0', 8 - substr_count($ip6, ':')).':', $ip6);

$ip6parts = explode(':', $ip6);

$res="";

foreach ($ip6parts as $part)

$res .= str_pad(base_convert( $part, 16, 2), 16, 0, STR_PAD_LEFT);

return $res;

}

/**

* MatchCIDR6 -- Check if an ipv6 address is in the CDIR6 subnet.

* @param string $cidr6 - an ipv6 subnet, ex 2001:288:5400/39 or 2001:288:5432:/64 or 2001:288:5478::/64..

* @param string $chkipv6 - an ipv6 address, ex ::1, 2001:288:5200::1, :: ,etc.

* @return return true if $chkipv6 is inside the $cidr6 subnet, or return false.

*/

public function MatchCIDR6( $cidr6, $chkipv6)

{

list($ip6, $prefixlen) = explode('/', $cidr6);

$cidrbin= substr( $this->ExpandIPv6Notation2Bin($ip6), 0, $prefixlen);

$chkip6bin= substr( $this->ExpandIPv6Notation2Bin($chkipv6), 0, $prefixlen);

if(! strcmp($cidrbin,$chkip6bin))return true;

return false;

}

}

用法

$cidr6 = "2001:288:5400/39";

$ip6= "2001:288:5300:000::72A:4BE7";

$o_ipv6 = new ipv6();

// 檢查是ip6 是否在網段內

if( $o_ipv6->MatchCIDR6($cidr6,$ip6))

print "Inside";

else

print "Outside";

備註:有參考別人的程式,不過來源佚失了

相關連結

原文 2009-11-26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java的InetAddress类来判断IP地址是否在同一网。以下是一个示例代码: ```java import java.net.InetAddress; import java.net.UnknownHostException; public class CheckIPv6InSameNetwork { public static void main(String[] args) { try { String ipv6Address = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; String gatewayAddress = "2001:0db8:85a3:0000:0000:8a2e:0370:733f"; int maskLength = 64; InetAddress ipv6 = InetAddress.getByName(ipv6Address); InetAddress gateway = InetAddress.getByName(gatewayAddress); byte[] ipv6Bytes = ipv6.getAddress(); byte[] gatewayBytes = gateway.getAddress(); // 判断前缀是否相同 boolean isSamePrefix = true; for (int i = 0; i < maskLength / 8; i++) { if (ipv6Bytes[i] != gatewayBytes[i]) { isSamePrefix = false; break; } } if (isSamePrefix) { // 判断后缀是否相同 boolean isSameSuffix = true; for (int i = maskLength / 8; i < ipv6Bytes.length; i++) { if (ipv6Bytes[i] != gatewayBytes[i]) { isSameSuffix = false; break; } } if (isSameSuffix) { System.out.println("IPv6地址和网关在同一网"); } else { System.out.println("IPv6地址和网关不在同一网"); } } else { System.out.println("IPv6地址和网关不在同一网"); } } catch (UnknownHostException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们假设IPv6地址和网关地址都已经以字符串的形式给出,并且子网掩码的长度为64。首先,我们使用`InetAddress.getByName()`方法将字符串转换为`InetAddress`对象。然后,我们将IPv6地址和网关地址转换为字节数组,以便我们可以比较它们的前缀和后缀。最后,我们比较前缀和后缀以确定它们是否在同一网

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值