【问题标题】:Creating IP hierarchical list in PHP在 PHP 中创建 IP 分层列表
【发布时间】:2013-04-14 09:50:18
【问题描述】:

我有一个存储 IP 地址的数据库表,如下所示:

 E_ID IP MASK

我想让它看起来像这样:

E_ID Parent_ID IP MASK

这样我就可以轻松获得我的 IP 地址树:

  80.17.0.0/18 (id=1 parent id = -1)
        80.17.0.0/24 (id=2, parent id=1)
            80.17.0.0/27 (id=3, parent id=2)
                80.17.0.0/31  (id=4, parent id=3)
                80.17.0.2/31  (id=5, parent id=3)
                80.17.0.4/31  (id=6, parent id=3)
                80.17.0.6/31  (id=7, parent id=3)
                80.17.0.8/31  (id=8, parent id=3)
                80.17.0.12/31  (id=9, parent id=3)
            80.17.0.32/27  (id=10, parent id=2)
                80.17.0.32/30 (id=11, parent id=10) 
                    80.17.0.32/32 (id=12, parent id=11) 
                    80.17.0.33/32 (id=13, parent id=11) 
                    80.17.0.34/32 (id=14, parent id=11) 
                    80.17.0.35/32 (id=15, parent id=11)     

所有 IP 和掩码都是 BINGINT (ip2long())。 我想出了什么:

$sql = "SELECT IP, Mask, E_ID
        FROM test
        ORDER BY `IP`, `Mask` ASC";

$result = mysqli_query($db, $sql) or die(mysqli_error($db));

$space = array();
$i = 0;
array_push($space, mysqli_fetch_assoc($result));

while($r = mysqli_fetch_assoc($result)){
    if(!ipvsnet(long2ip($r['IP']), long2ip($space[$i]['IP']), long2ip($space[$i]['Mask']))){
        array_push($space, $r);
        $i++;
    }

function ipvsnet($ip, $network, $mask){
    if(((ip2long($ip))&(ip2long($mask))) == ip2long($network)){
        return 1;
        } else {
        return 0;
    }
}

这只给了我主要的父母地址。我不明白如何找到更深的节点,因为我不知道每个分支有多深。提前谢谢你。

【问题讨论】:

    标签: php algorithm list ip hierarchical


    【解决方案1】:

    从外观上看,您尝试按网络 ID、子网掩码和节点 ID 对 IP 地址进行排序/组织。我认为您误解了子网掩码在 IP 寻址中的作用(这是一个非常常见的错误,我曾经也犯过这个错误 :))。

    子网掩码用于将网络 ID 与 IP 地址分开。子网掩码是一个二进制数,由一系列不间断的 1 位组成,从二进制字的最高有效位(4 字节二进制值)开始。例如:

    11110000.00000000.00000000.00000000 // subnet_mask: /4
    11111000.00000000.00000000.00000000 // subnet_mask: /5
    11011000.00000000.00000000.00000000 // not a legal bitmask because of the 0 bit
    

    当您考虑诸如 74.125.224.197 之类的随机 IP 地址时(在这里做一点数学运算),我们得到位模式:

    01001010.01111101.11100000.11000101 // This is the same as 74.125.224.197
    

    现在,如果我们有这个特定 IP 的子网掩码:

    11110000.00000000.00000000.00000000
    

    那么网络ID将是子网掩码和IP地址的逻辑与:

        01001010.01111101.11100000.11000101 // ip
      & 11110000.00000000.00000000.00000000 // subnet mask
    ---------------------------------------
        01000000.00000000.00000000.00000000 // Network Id
    

    按照逻辑相似的路线,节点 ID 将是 IP 地址和子网掩码的 /negation/ 的逻辑与:

    /* negation of subnet mask via XOR */
        11110000.00000000.00000000.00000000 // subnet mask
    XOR 11111111.11111111.11111111.11111111 
    ---------------------------------------
        00001111.11111111.11111111.11111111 // negation of subnet mask
    
    /* getting the node id from the IP address using the negation of subnet mask */
        00001111.11111111.11111111.11111111 // negation of subnet mask
    &   01001010.01111101.11100000.11000101 // original IP address
    ---------------------------------------
        00001010.01111101.11100000.11000101 // the node Id
    

    所有这一切的重点是与您对子网掩码的性质以及您的排序顺序的理解相矛盾。考虑这样的子网掩码:

        01001010.01111101.11100000.11000101 // original IP address of google
    &   11000000.00000000.00000000.00000000 // different subnet mask
    ---------------------------------------
        01000000.00000000.00000000.00000000 // same network id(sort of) as above
    

    我们如何区分这次生成的内容与上次计算网络 ID 时生成的内容。答案与子网掩码有关。子网掩码实际上是关于确定网络 ID。 IP 地址和子网掩码应该一起考虑,因为您需要两者来确定网络 id(这就是您尝试排序的样子)。

    如果您想组织数据以进行有意义的操作,请考虑首先将原始 IP 地址 + 子网掩码转换为正确的网络 ID 和节点 ID 部分。其余的将很快为您准备就绪。

    我真的希望这对人有所帮助!

    【讨论】:

    • 谢谢@bool32,我想出了解决方案,100% 满足我的需求:$sql = "SELECT E_ID, IP, Mask FROM test ORDER BY IP, Mask ASC"; $result = mysqli_query($db, $sql) or die(mysqli_error($db)); while($r = mysqli_fetch_assoc($result)){ $sql = sprintf("UPDATE test SET Parent_ID = '%s' WHERE IP&'%s' = '%s' AND Mask > '%s' AND Parent_ID != -1", $r['E_ID'], $r['Mask'], $r['IP'], $r['Mask']); mysqli_query($db, $sql) or die(mysqli_error($db)); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多