I have ip and I want to convert it into CIDR block with specific mask [/28, for example]. Using PHP.
So that input "1.1.1.100" and "28" and output is "1.1.1.96/28"
ip_to_cidr['1.1.1.100', '28'] // '1.1.1.96/28'
I found a lot of functions to match and ranges, but nothing about actually converting from ip to CIDR. Seems like to be most basic operation, may be I am missing something, using wrong search terms or misunderstanding masks.
For context: I need to block bots, I have their IP and want to block range in ipset. /24 is a bit to much [but easy to make, just replace last bit with ".0/24"], so I want to use /27 /28 /29.
asked Feb 29, 2020 at 4:28
QiaoQiao
16.1k28 gold badges86 silver badges117 bronze badges
It's ... beautiful
function ip_to_mask28[$ip]
{
$ip_parts = explode['.', $ip];
$mask28 = [0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256];
foreach [ $mask28 as $key => $value ]
if [ $ip_parts[3] < $mask28[$key + 1] ]
return $ip_parts[0].'.'.$ip_parts[1].'.'.$ip_parts[2].'.'.$value.'/28';
}
echo ip_to_mask28['1.1.1.100']; // 1.1.1.96/28
Rolling with this now
answered Feb 29, 2020 at 5:17
QiaoQiao
16.1k28 gold badges86 silver badges117 bronze badges
Is this what you are looking for ? I searched about it and found this which is close to your output. you can find more information here
function ipRange[$cidr] {
$range = array[];
$cidr = explode['/', $cidr];
$range[0] = long2ip[[ip2long[$cidr[0]]] & [[-1
string[8] "1.1.1.96"
[1]=>
string[9] "1.1.1.111"
}
answered Feb 29, 2020 at 5:15
Umer AbbasUmer Abbas
1,7863 gold badges12 silver badges18 bronze badges
1
Not the answer you're looking for? Browse other questions tagged php ip cidr or ask your own question.
Conversion table for CIDR notation.
/1 | 128.0.0.0 | 80 00 00 00 | 127.255.255.255 | 1000 0000 0000 0000 0000 0000 0000 0000 | 128 As | 2,147,483,646 | |
/2 | 192.0.0.0 | C0 00 00 00 | 63.255.255.255 | 1100 0000 0000 0000 0000 0000 0000 0000 | 64 As | 1,073,741,822 | |
/3 | 224.0.0.0 | E0 00 00 00 | 31.255.255.255 | 1110 0000 0000 0000 0000 0000 0000 0000 | 32 As | 536,870,910 | |
/4 | 240.0.0.0 | F0 00 00 00 | 15.255.255.255 | 1111 0000 0000 0000 0000 0000 0000 0000 | 16 As | 268,435,454 | |
/5 | 248.0.0.0 | F8 00 00 00 | 7.255.255.255 | 1111 1000 0000 0000 0000 0000 0000 0000 | 8 As | 134,217,726 | |
/6 | 252.0.0.0 | FC 00 00 00 | 3.255.255.255 | 1111 1100 0000 0000 0000 0000 0000 0000 | 4 As | 67,108,862 | |
/7 | 254.0.0.0 | FE 00 00 00 | 1.255.255.255 | 1111 1110 0000 0000 0000 0000 0000 0000 | 2 As | 33,554,430 | |
/8 | 255.0.0.0 | FF 00 00 00 | 0.255.255.255 | 1111 1111 0000 0000 0000 0000 0000 0000 | 1 A or 256 Bs | 16,777,214 | |
/9 | 255.128.0.0 | FF 80 00 00 | 0.127.255.255 | 1111 1111 1000 0000 0000 0000 0000 0000 | 128 Bs | 8,388,606 | |
/10 | 255.192.0.0 | FF C0 00 00 | 0.63.255.255 | 1111 1111 1100 0000 0000 0000 0000 0000 | 64 Bs | 4,194,302 | |
/11 | 255.224.0.0 | FF E0 00 00 | 0.31.255.255 | 1111 1111 1110 0000 0000 0000 0000 0000 | 32 Bs | 2,097,150 | |
/12 | 255.240.0.0 | FF F0 00 00 | 0.15.255.255 | 1111 1111 1111 0000 0000 0000 0000 0000 | 16 Bs | 1,048,574 | |
/13 | 255.248.0.0 | FF F8 00 00 | 0.7.255.255 | 1111 1111 1111 1000 0000 0000 0000 0000 | 8 Bs | 524,286 | |
/14 | 255.252.0.0 | FF FC 00 00 | 0.3.255.255 | 1111 1111 1111 1100 0000 0000 0000 0000 | 4 Bs | 262,142 | |
/15 | 255.254.0.0 | FF FE 00 00 | 0.1.255.255 | 1111 1111 1111 1110 0000 0000 0000 0000 | 2 Bs | 131,070 | |
/16 | 255.255.0.0 | FF FF 00 00 | 0.0.255.255 | 1111 1111 1111 1111 0000 0000 0000 0000 | 1 B or 256 Cs | 65,534 | |
/17 | 255.255.128.0 | FF FF 80 00 | 0.0.127.255 | 1111 1111 1111 1111 1000 0000 0000 0000 | 128 Cs | 32,766 | |
/18 | 255.255.192.0 | FF FF C0 00 | 0.0.63.255 | 1111 1111 1111 1111 1100 0000 0000 0000 | 64 Cs | 16,382 | |
/19 | 255.255.224.0 | FF FF E0 00 | 0.0.31.255 | 1111 1111 1111 1111 1110 0000 0000 0000 | 32 Cs | 8,190 | |
/20 | 255.255.240.0 | FF FF F0 00 | 0.0.15.255 | 1111 1111 1111 1111 1111 0000 0000 0000 | 16 Cs | 4,094 | |
/21 | 255.255.248.0 | FF FF F8 00 | 0.0.7.255 | 1111 1111 1111 1111 1111 1000 0000 0000 | 8 Cs | 2,046 | |
/22 | 255.255.252.0 | FF FF FC 00 | 0.0.3.255 | 1111 1111 1111 1111 1111 1100 0000 0000 | 4 Cs | 1,022 | |
/23 | 255.255.254.0 | FF FF FE 00 | 0.0.1.255 | 1111 1111 1111 1111 1111 1110 0000 0000 | 2 Cs | 510 | |
/24 | 255.255.255.0 | FF FF FF 00 | 0.0.0.255 | 1111 1111 1111 1111 1111 1111 0000 0000 | 1 C | 254 | |
/25 | 255.255.255.128 | FF FF FF 80 | 0.0.0.127 | 1111 1111 1111 1111 1111 1111 1000 0000 | 1/2 C | 126 | |
/26 | 255.255.255.192 | FF FF FF C0 | 0.0.0.63 | 1111 1111 1111 1111 1111 1111 1100 0000 | 1/4 C | 62 | |
/27 | 255.255.255.224 | FF FF FF E0 | 0.0.0.31 | 1111 1111 1111 1111 1111 1111 1110 0000 | 1/8 C | 30 | |
/28 | 255.255.255.240 | FF FF FF F0 | 0.0.0.15 | 1111 1111 1111 1111 1111 1111 1111 0000 | 1/16 C | 14 | |
/29 | 255.255.255.248 | FF FF FF F8 | 0.0.0.7 | 1111 1111 1111 1111 1111 1111 1111 1000 | 1/32 C | 6 | |
/30 | 255.255.255.252 | FF FF FF FC | 0.0.0.3 | 1111 1111 1111 1111 1111 1111 1111 1100 | 1/64 C | 2 | |
/31 | 255.255.255.254 | FF FF FF FE | 0.0.0.1 | 1111 1111 1111 1111 1111 1111 1111 1110 | 1/128 C | 0 | |
/32 | 255.255.255.255 | FF FF FF FF | 0.0.0.0 | 1111 1111 1111 1111 1111 1111 1111 1111 | 1/256 C | 1 |