php替代ip2long / long2ip

时间:2013-04-25 06:01:03

标签: php math ip

我一直试图想出一种方法来将IP地址存储为PHP的无符号整数,但我想我对如何提出正确的函数并不熟悉。这是我想出的(错误的):

// ip 2 unsigned int
function ip2int($ipaddress) {
    // turns IP into signed integer
    $signed = ip2long($ipaddress);
    $unsigned = sprintf("%u", $signed);
    return $unsigned;
    }

// unsigned int 2 ip
function int2ip($unsigned) {
    $signed = sprintf("%d", $unsigned);
    $ipaddress = long2ip($signed);
    return $ipaddress;
    }

有关如何解决此问题的任何建议吗?

1 个答案:

答案 0 :(得分:0)

long2ip将整数转换为IP格式,而ip2long则反之亦然 像这样使用

$long = ip2long($_SERVER['REMOTE_ADDR']);//or your ip
$ip   = long2ip($long);