亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2043 | 回復(fù): 0
打印 上一主題 下一主題

PHP對IP地址和子網(wǎng)掩碼的處理方法 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-06-24 23:27 |只看該作者 |倒序瀏覽
ip2long IP地址轉(zhuǎn)換成整型。
long2ip 整型數(shù)據(jù)轉(zhuǎn)換成IP。
子網(wǎng)掩碼轉(zhuǎn)換成掩碼長度方式:
$slash_notation = strlen(preg_replace("/0/", "", decbin(ip2long($subnet_mask))));
$bits=strpos(decbin(ip2long($mask)),"0");
子網(wǎng)掩碼位長轉(zhuǎn)換成子網(wǎng)掩碼形式:
$mask = 0xffffffff $mask = pow(2,32)-pow(2,(32-$mask));
判斷兩個地址是否在一個子網(wǎng)內(nèi):
function matchCIDR($addr, $cidr) {
   list($ip, $mask) = explode('/', $cidr);
   return (ip2long($addr) >> (32 - $mask) == ip2long($ip) >> (32 - mask));
}
?>
一個IPv4的類:
//--------------
// IPv4 class
class ipv4
{
  var $address;
  var $netbits;
   //--------------
  // Create new class
  function ipv4($address,$netbits)
  {
    $this->address = $address;
    $this->netbits = $netbits;
  }
   //--------------
  // Return the IP address
  function address() { return ($this->address); }
   //--------------
  // Return the netbits
  function netbits() { return ($this->netbits); }
   //--------------
  // Return the netmask
  function netmask()
  {
    return (long2ip(ip2long("255.255.255.255")
           32-$this->netbits)));
  }
   //--------------
  // Return the network that the address sits in
  function network()
  {
    return (long2ip((ip2long($this->address))
           & (ip2long($this->netmask()))));
  }
   //--------------
  // Return the broadcast that the address sits in
  function broadcast()
  {
    return (long2ip(ip2long($this->network())
           | (~(ip2long($this->netmask())))));
  }
   //--------------
  // Return the inverse mask of the netmask
  function inverse()
  {
    return (long2ip(~(ip2long("255.255.255.255")
           32-$this->netbits))));
  }
}
  $ip = new ipv4("192.168.2.1",24);
  print "Address: $ip->address()\n";
  print "Netbits: $ip->netbits()\n";
  print "Netmask: $ip->netmask()\n";
  print "Inverse: $ip->inverse()\n";
  print "Network: $ip->network()\n";
  print "Broadcast: $ip->broadcast()\n";
?>
這個做法比較有創(chuàng)意:
For those poor little people using PHP 3, here's an ip2long:
if (!function_exists("ip2long")) {
function ip2long($ip) {
  $ex = explode(".", $ip);
  if (count($ex)!=4) return -1;
  list($a, $b, $c, $d) = $ex;
  $a = $a*16777216;
  $b = $b*65536;
  $c = $c*256;
  return $a+$b+$c+$d;
}
}
?>
#!/usr/local/bin/php
" . long2ip($bc - 1)  . "\n";
?>
Produces the output:
IP Address:         172.14.1.57
Subnet Mask:        255.255.255.0
Network Address:    172.14.1.0
Broadcast Address:  172.14.1.255
Number of Hosts:    254
Host Range:         172.14.1.1 -> 172.14.1.254
               
               
               

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u/2389/showart_1010530.html
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP