博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP对IP地址和子网掩码的处理方法
阅读量:7081 次
发布时间:2019-06-28

本文共 2724 字,大约阅读时间需要 9 分钟。

hot3.png

ip2long IP地址转换成整型。

long2ip 整型数据转换成IP。

子网掩码转换成掩码长度方式:

$slash_notation = strlen(preg_replace("/0/", "", decbin(ip2long($subnet_mask))));
$bits=strpos(decbin(ip2long($mask)),"0");

子网掩码位长转换成子网掩码形式:

$mask = 0xffffffff << (32 - $mask);
$mask = pow(2,32)-pow(2,(32-$mask));

判断两个地址是否在一个子网内:

<?php
function matchCIDR($addr, $cidr) {
   list($ip, $mask) = explode('/', $cidr);
   return (ip2long($addr) >> (32 - $mask) == ip2long($ip) >> (32 - mask));
}
?>

一个IPv4的类:

<?php

 //--------------

// 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";
?>

这个做法比较有创意:

For those poor little people using PHP 3, here's an ip2long:
<?php
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

<?
   $ip_addr = "172.14.1.57";
   $subnet_mask = "255.255.255.0";

   $ip = ip2long($ip_addr);

   $nm = ip2long($subnet_mask);
   $nw = ($ip & $nm);
   $bc = $nw | (~$nm);

   echo "IP Address:         " . long2ip($ip) . "\n";

   echo "Subnet Mask:        " . long2ip($nm) . "\n";
   echo "Network Address:    " . long2ip($nw) . "\n";
   echo "Broadcast Address:  " . long2ip($bc) . "\n";
   echo "Number of Hosts:    " . ($bc - $nw - 1) . "\n";
   echo "Host Range:         " . long2ip($nw + 1) . " -> " . 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

转载于:https://my.oschina.net/u/141224/blog/339134

你可能感兴趣的文章
JAVA课堂作业整理一
查看>>
程序猿小白的2016—不忘初心,继续奋斗
查看>>
HDU 1502 Regular Words DP+高精度
查看>>
实验指令(5)
查看>>
矩阵最优连乘问题
查看>>
Eclipse 调试 Java 程序的技巧
查看>>
TCP/IP详解--发送ACK和RST的场景
查看>>
JS基础(二)
查看>>
xcode6 swift 没法自动补全和高亮的解决方法
查看>>
大二上期课表
查看>>
Eclipse之调试代码和返回
查看>>
VIM键盘映射 (Map)~转载
查看>>
移动端缩放设置
查看>>
GCC编译动态和静态链接库例子
查看>>
道格拉斯-普克抽稀算法《转》
查看>>
BZOJ 1002 轮状病毒 矩阵树定理
查看>>
python之paramiko 远程执行命令
查看>>
materialized view 和snapshot
查看>>
PHP使用数据库的并发问题(转)
查看>>
关于tcc、tlink的编译链接机制的研究
查看>>