简体   繁体   English

如何使用PHP简化IP地址? 尝试long2ip(ip2long(address))

[英]How can I simplify an IP address using PHP? Trying long2ip(ip2long(address))

My system includes a third party box that stores IP addresses and compares them to values sent from my Web interface. 我的系统包括一个第三方框,用于存储IP地址并将其与从我的Web界面发送的值进行比较。 I have a problem that a user entered the netmask for the third party box as 255.255.255.000, and the box stored it as 255.255.255.0. 我有一个问题,用户输入第三方框的网络掩码为255.255.255.000,并且该框将其存储为255.255.255.0。 However the next time the box reboots and is told that the mask includes 000 it concludes that these values are different and updates its database and reboots. 但是,下次该框重新启动并被告知掩码包含000时,它得出以下结论:这些值不同,并更新其数据库并重新启动。 And reboots. 并重新启动。 And reboots. 并重新启动。

My intent is to solve this by storing the netmask in my own system in the simplified form 255.255.255.0 (and as another example store 010.001.002.005 as 10.1.2.5). 我的目的是通过将网络掩码以简化形式255.255.255.0存储在我自己的系统中(并作为另一个示例将010.001.002.005存储为10.1.2.5)来解决此问题。 But I'm brand new to PHP. 但是我是PHP的新手。 I tried code $mask = long2ip(ip2long($mask)) to convert the entry to a standardized form but just got back 0.0.0.0. 我尝试使用代码$mask = long2ip(ip2long($mask))将条目转换为标准化形式,但刚刚返回0.0.0.0。 Is there something special I need to do to convert the IP to long and back? 我需要做些特殊的事情来将IP转换为长时转换吗? Is there a better way to simplify this? 有没有更好的方法可以简化此过程?

ip2long returns false on an error and it doesn't seem to detect 000 as valid. ip2long在发生错误时返回false,并且似乎没有检测到000为有效。 Since 255.255.255.000 isn't detected as a valid IP, it will return false, so long2ip(false) won't spit out a valid IP. 由于未将255.255.255.000检测为有效IP,因此它将返回false,因此long2ip(false)不会吐出有效IP。

You could just check that ip2long(input) returns true before even accepting the IP address. 您甚至可以在接受IP地址之前检查ip2long(input)返回true。 Having proper validation in place would prevent invalid IPs from breaking the system. 进行适当的验证将防止无效的IP破坏系统。 Trying to implement auto-correction for all of the possible invalid IP addresses is going to be much harder than just enforcing a valid IP to begin with. 试图对所有可能的无效IP地址实施自动校正要比仅仅强制执行一个有效IP困难得多。

To follow the suggestion by @ka_lin in the comments, just explode it into parts, convert it to an int which will drop leading 0's and rebuild it with implode... 要遵循@ka_lin在评论中的建议,只需将其分解为多个部分,将其转换为将删除前导0的int并进行爆破重建即可。

$mask = implode(".", array_map("intval", explode(".", $mask)));

A simple and less overhead version assumes 4 parts to the IP address... 一个简单且开销较小的版本假定IP地址由4部分组成...

list ($o1, $o2, $o3, $o4) = explode(".", $mask);
$mask = (int)$o1.".".(int)$o2.".".(int)$o3.".".(int)$o4;

It is difficult as 010.001.002.005 is a valid IP address, but could also be confusing (sometimes numbers starting with a 0 are octal). 很难做到,因为010.001.002.005是有效的IP地址,但也可能造成混淆(有时以0开头的数字是八进制的)。

A note: ip2long() has an issue: It doesn't accept the valid ip address 127.1 (classic a notation, but valid!) or 127.0.0.010 (RFC says invalid, but most read 010 as octal number). 注意: ip2long()有一个问题:它不接受有效的IP地址127.1 (经典的表示法,但是有效!)或127.0.0.010 (RFC表示无效,但大多数将010作为八进制数读取)。

The alternative if gethostbyname() . 替代方法gethostbyname() But gethostbyname() has issues too: If an invalid ip is entered (like 127.0.0.1234 ), it makes a DNS lookup and returns the source, if the lookup fails. 但是gethostbyname()也存在问题:如果输入了无效的ip(如127.0.0.1234 ),则它将进行DNS查找,如果查找失败,则返回源。

A solution can be: 解决方案可以是:

long2ip(ip2long(gethostbyname($IP_ADDRESS)))

My personal solution is the following function: 我的个人解决方案是以下功能:

function aton ( $addr )
{
    $l = explode('.',$addr);
    switch (count($l))
    {
        case 0: return FALSE;

        case 1: return intval($l[0]) & 0xffffffff;

        case 2: return  ( intval($l[0]) << 24
                        | intval($l[1])
                        ) & 0xffffffff;

        case 3: return  ( intval($l[0]) << 24
                        | intval($l[1]) << 16
                        | intval($l[2])
                        ) & 0xffffffff;

        default: return ( intval($l[0]) << 24
                        | intval($l[1]) << 16
                        | intval($l[2]) <<  8
                        | intval($l[3])
                        ) & 0xffffffff;
    }
}

With this function, you can use: 通过此功能,您可以使用:

long2ip(aton($IP_ADDRESS))

btw, I had never issues with special ip addresses like 255.255.255.0 . 顺便说一句,我从来没有问题,如255.255.255.0特殊IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM