Home How to calculate IP address range?
Reply: 1

How to calculate IP address range?

Community
1#
Community Published in 2013-09-30 10:06:31Z

This question already has an answer here:

  • calculate IP range using PHP and CIDR 3 answers

I want to calculate IP ranges for a specific IP and Subnet using PHP, I mean that I want to calculate Host address range, for example:

IP: 91.99.98.243
Subnet: 255.255.255.240

Then:

Host Address Range: 91.99.98.241 - 91.99.98.254

How can I calculate this range using ip2long()?

Community
2#
Community Reply to 2017-05-23 11:47:25Z

If you want to get the address range only, you can use bitwise operators on the long addresses:

$ip     = ip2long('91.99.98.243');
$mask   = ip2long('255.255.255.240');
echo 'Host Address Range: ', long2ip($ip & $mask), ' - ', long2ip($ip | ~$mask);

If you want to loop on each address, see calculate IP range using PHP and CIDR as Joran Den Houting mentioned.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.383924 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO