Created
October 29, 2021 11:59
-
-
Save mgilank/b7fddcb2e4ff8dd2b9cde6a26f2fa228 to your computer and use it in GitHub Desktop.
tear ipv6
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 | |
$ip = '2001:0df0:021f:0000:0000:0000:0000:0000'; | |
$cidrnet = '2001:df0:21f::/48'; | |
function inet_to_bits($inet) | |
{ | |
$splitted = str_split($inet); | |
$binaryip = ''; | |
foreach ($splitted as $char) { | |
$binaryip .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT); | |
} | |
return $binaryip; | |
} | |
$ip = inet_pton($ip); | |
$binaryip=inet_to_bits($ip); | |
list($net,$maskbits)=explode('/',$cidrnet); | |
$net=inet_pton($net); | |
$binarynet=inet_to_bits($net); | |
$ip_net_bits=substr($binaryip,0,$maskbits); | |
$net_bits =substr($binarynet,0,$maskbits); | |
if($ip_net_bits!==$net_bits) echo 'Not in subnet'; | |
else echo 'In subnet'; | |
// source https://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment