欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php求一個網(wǎng)段開始與結(jié)束IP地址的方法

 更新時間:2015年07月09日 12:23:09   作者:kevin0216  
這篇文章主要介紹了php求一個網(wǎng)段開始與結(jié)束IP地址的方法,涉及php字符串操作與進(jìn)制轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了php求一個網(wǎng)段開始與結(jié)束IP地址的方法。分享給大家供大家參考。具體如下:

比如:網(wǎng)段(192168.1.5/24),其子網(wǎng)掩碼根據(jù)24劃分為:
11111111.11111111.11111111.00000000(255.255.255.0)
說明:IP地址為32bits,24在劃分網(wǎng)段中就表示前面有24個1,后面有8個0.
開始IP地址的算法是:192.168.1.5的二進(jìn)制與子網(wǎng)掩碼的二進(jìn)制進(jìn)行“與”運算出來的。
結(jié)束IP地址的算法是:子網(wǎng)掩碼的二進(jìn)制先取反,然后和192.168.1.5的二進(jìn)制進(jìn)行“或”運算
實際的就應(yīng)用中,那樣得出的是網(wǎng)絡(luò)地址和廣播地址,網(wǎng)絡(luò)地址+1才是第一個主機(jī)地址,廣播地址-1為最后一個主機(jī)地址。

<?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');//設(shè)定IP地址,可以從表單獲取,這里只作演示
$mask = '24';          //設(shè)置掩碼
$bSub = mask2bin($mask);     //將子網(wǎng)掩碼轉(zhuǎn)換二進(jìn)制
$mask = array();
$mask[] = substr($bSub,"0",8);  //將子網(wǎng)掩碼每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">網(wǎng)絡(luò)地址:</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 ".";
  }
?>

希望本文所述對大家的php程序設(shè)計有所幫助。

相關(guān)文章

最新評論