Php is used to obtain the start and end IP addresses of a CIDR block.

Source: Internet
Author: User

Php is used to obtain the start and end IP addresses of a CIDR block.

This example describes how to obtain the start and end IP addresses of a network segment in php. Share it with you for your reference. The details are as follows:

For example, for a network segment (192168.1.5/24), its subnet mask is divided:
11111111.11111111.11111111.00000000 (255.255.255.0)
Note: If the IP address is 32 bits, 24 indicates that there are 24 first 1 and 8 are later 0 in the network segment division.
The algorithm for starting an IP address is: the binary of 192.168.1.5 and the binary of the subnet mask.
The algorithm for ending an IP address is: The subnet mask is first reversed, and then "or" is performed with the 192.168.1.5 binary.
In practice, the network address and broadcast address are obtained as follows. network address + 1 is the first host address, and broadcast address-1 is the last host address.

<? Phpfunction mask2bin ($ n) {$ n = intval ($ n); if ($ n <0 | $ n> 32) die ('error submask '); return str_repeat ("1", $ n ). str_repeat ("0", 32-$ n);} function revBin ($ s) {$ p = array ('0', '1', '2 '); $ r = array ('2', '0', '1'); return str_replace ($ p, $ r, $ s);} function startIp ($ str, $ bSub) {$ bIp = decbin ($ str); $ bIp = str_pad ($ bIp, 8, "0", STR_PAD_LEFT ); $ sIp = bindec ($ bIp & $ bSub); return $ sIp;} function endIp ($ str, $ bSub) {$ bIp = decb In ($ str); $ bIp = str_pad ($ bIp, 8, "0", STR_PAD_LEFT); $ eIp = bindec ($ bIp | revBin ($ bSub )); return $ eIp;} $ ip = array ('20160301', '20160301', '1', '5'); // you can obtain the ip address from the form, here we will only demonstrate $ mask = '24'; // set the mask $ bSub = mask2bin ($ mask); // convert the subnet mask to binary $ mask = array (); $ mask [] = substr ($ bSub, "0", 8); // divide the subnet mask into segments every 8 bits $ mask [] = substr ($ bSub, "8", 8); $ mask [] = substr ($ bSub, "16", 8); $ mask [] = substr ($ bSub, "24 ", 8); echo '<table summary = "result" bord Er = "1" cellspacing = "1" cellpadding = "0"> <tbody> <td align = "right"> <font size = "2"> mask: </font> </td> <font size = "2"> '; for ($ I = 0; $ I <4; $ I ++) {echo bindec ($ mask [$ I]); if ($ I! = 3) echo ". ";} echo '</font> </td> </tr> <td align =" right "> <font size =" 2 "> network address: </font> </td> <font size = "2"> '; for ($ I = 0; $ I <4; $ I ++) {echo startIp ($ ip [$ I], $ mask [$ I]); if ($ I! = 3) echo ". ";} echo '</font> </td> </tr> <td align = "right"> <font size = "2"> first available: </font> </td> <font size = "2"> '; for ($ I = 0; $ I <3; $ I ++) {echo startIp ($ ip [$ I], $ mask [$ I]); echo ". ";}$ ip_4 = startIp ($ ip [3], $ mask [3]); echo ++ $ ip_4; echo '</font> </td> </tr> <td align = "right"> <font size = "2"> available at last: </font> </td> <font size = "2"> '; for ($ I = 0; $ I <3; $ I ++) {echo endIp ($ ip [$ I], $ mask [$ I]); echo ". ";}$ ip_4 = endIp ($ ip [3], $ mask [3]); echo -- $ ip_4; echo '</font> </td> </tr> <td align = "right"> <font size = "2"> broadcast address: </font> </td> <font size = "2"> '; for ($ I = 0; $ I <4; $ I ++) {echo endIp ($ ip [$ I], $ mask [$ I]); if ($ I! = 3) echo "." ;}?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.