2

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()?

  • 1
    No, it is not my answer. for my case it calculate range as: 91.99.98.243 - 91.99.99.2 and it is incorrect –  Sep 30 '13 at 10:24

1 Answers1

2

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.

Community
  • 1
  • 1
pozs
  • 34,608
  • 5
  • 57
  • 63
  • You need to exclude last address from array (since it will be network address according to IP protocol) – Alma Do Sep 30 '13 at 10:24