Created
June 25, 2014 22:08
-
-
Save Ikke/e12191d2efd0e0a4f3cc to your computer and use it in GitHub Desktop.
Checks if ipv6 address is inside a range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function ipv6_cidr($subnet, $range, $ip) | |
{ | |
$bit_mask = mask_bits(128, $range); | |
$subnet = inet_pton($subnet) & $bit_mask; | |
return (inet_pton($ip) & $bit_mask) == $subnet; | |
} | |
function mask_bits($total_length, $mask_length) | |
{ | |
$one_bits = ((int) $mask_length / 8) * 8; | |
$part_bits = 8 - ($mask_length % 8); | |
$zero_bits = $total_length - $one_bits - 1; | |
$mask = str_repeat(chr(0xff), $one_bits / 8) . chr(256 - pow(2, $part_bits)) . str_repeat(chr(0), $zero_bits / 8); | |
return $mask; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment