PHP to find a network segment start and end IP address method _php tips

Source: Internet
Author: User
Tags php programming

The example of this article tells the PHP method to find a network segment start and end IP address. Share to everyone for your reference. Specifically as follows:

For example: Network segment (192168.1.5/24), its subnet mask according to 24 divided into:
11111111.11111111.11111111.00000000 (255.255.255.0)
Description: IP address for 32bits,24 in the division of the network segment means that the front has 24 1, followed by 8 0.
The beginning of the IP address algorithm is: 192.168.1.5 binary and subnet mask binary for "and" operation.
The algorithm for ending an IP address is that the binary of the subnet mask is first reversed and then "or" with the 192.168.1.5 binary
The actual application, that is the network address and broadcast address, network address +1 is the first host address, broadcast address-1 for the last host address.

<?php function 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 = Decbin ($STR);
  $bIp = Str_pad ($bIp, 8, "0", str_pad_left);
  $eIp = Bindec ($bIp | revbin ($bSub));
return $eIp;          $ip = Array (' 192 ', ' 168 ', ' 1 ', ' 5 '),//Set IP address, can be obtained from the form, here only for demonstration $mask = ' 24 ';     Set Mask $bSub = Mask2bin ($mask);
Converts the subnet mask into binary $mask = array ();  $mask [] = substr ($bSub, "0", 8);
Divide the subnet mask every 8 bits $mask [] = substr ($bSub, "8", 8);
$mask [] = substr ($bSub, "16", 8);
$mask [] = substr ($bSub, "24", 8); Echo ' <table summary= "result" border= "1" cellspacing= "1" cellpadding= "0" > <tbody> <td align= "right" ; <font size= "2" > Mask: &Lt;/font></td> <td> <font size= "2" > ";
  For ($i =0 $i <4; $i + +) {echo bindec ($mask [$i]);
if ($i!=3) echo "."; Echo ' </font> </td> </tr> <tr> <td align= "right" ><font size= "2" > Network address: </fon
  t></td> <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> </tr> <tr> <td align= ' right ' >&lt
  ; font size= "2" > First available:</font></td> <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> <tr> <td align= "right" ><font size= "2" > Last available: </font
  ></td> <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> <tr> <td align= "right" ><font size= "2" > Broadcast address: &LT;/FO
  nt></td> <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 your 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.