Home > Backend Development > PHP Tutorial > How to find the starting and ending IP address of a network segment in php, phpip_PHP tutorial

How to find the starting and ending IP address of a network segment in php, phpip_PHP tutorial

WBOY
Release: 2016-07-13 09:46:55
Original
1029 people have browsed it

php method to find the start and end IP address of a network segment, phpip

This article describes the example of php method to find the start and end IP address of a network segment. Share it with everyone for your reference. The details are as follows:

For example: network segment (192168.1.5/24), its subnet mask is divided according to 24:
11111111.11111111.11111111.00000000(255.255.255.0)
Note: The IP address is 32 bits. 24 in the network segment means that there are 24 1s in the front and 8 0s in the back.
The algorithm of the starting IP address is: the binary number of 192.168.1.5 is ANDed with the binary number of the subnet mask.
The algorithm for ending the IP address is: first invert the binary number of the subnet mask, and then perform an "OR" operation with the binary number of 192.168.1.5
In actual applications, the network address and broadcast address are obtained. The network address 1 is the first host address, and the broadcast address -1 is the last host address.

<&#63;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');//设定IP地址,可以从表单获取,这里只作演示
$mask = '24';          //设置掩码
$bSub = mask2bin($mask);     //将子网掩码转换二进制
$mask = array();
$mask[] = substr($bSub,"0",8);  //将子网掩码每8位分一段
$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">掩码:</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">网络地址:</font></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"><font size="2">第一个可用:</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">最后可用:</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">广播地址:</font></td>
  <td>
  <font size="2">';
  for ($i=0;$i<4;$i++)
  {
   echo endIp($ip[$i],$mask[$i]);
   if($i!=3)
   echo ".";
  }
&#63;>

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1029594.htmlTechArticlephp method to find the start and end IP address of a network segment, phpip This article tells the example of php to find the start of a network segment with the method of ending the IP address. Share it with everyone for your reference. The details are as follows:...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template