天天看點

PHP中有關IPV4 和IPV6位址轉換以及其它一些常見問題

這裡主要介紹一下 IPV4 / IPV6 在 PHP / MySQL 中如何轉換。以及中間容易碰到的一些問題。

  • 首先介紹兩個函數:

ip2long:将 IPV4 的字元串網際網路協定轉換成長整型數字

int ip2long ( string $ip_address )      

long2ip:将長整型轉化為字元串形式帶點的網際網路标準格式位址(IPV4)

string long2ip ( int $proper_address )      

問題一:MySQL 中如何存儲IP位址。

  • IPV4 位址長度32位,有 2^32-1 個位址。 是以 MySQL 中如果使用 int 來存儲,要加 unsigned 辨別。
  • int 有符号的範圍是 -2^31 (-2,147,483,648) 到 2^31 - 1 (2,147,483,647) ,無符号的範圍是 0 到 2^32-1(4294967295)
  • IPV6 位址長度128位。是以不能使用 int 存儲,可以使用 varchar 類型存儲。

問題二:ip2long 出現負數問題。

示例:

$ip_long = ip2long('192.168.8.30');
$long_ip = long2ip($ip_long);
echo $ip_long;    // -1062729698
echo $long_ip; // 192.168.8.30      

檢視PHP手冊後,發現手冊上是這麼介紹的:

Because PHP’s integer type is signed, and many IP addresses will result in negative integers on 32-bit architectures, you need to use the “%u” formatter of sprintf() or printf() to get the string representation of the unsigned IP address.

因為PHP的 integer 類型是有符号,并且有許多的IP位址将導緻在32位系統的情況下為負數, 你需要使用 “%u” 進行轉換通過 sprintf() 或printf() 得到的字元串來表示無符号的IP位址。

解決辦法:

$ip_long = sprintf('%u', ip2long('192.168.8.30'));
echo $ip_long; // 3232237598      

接着又發現一個新問題,如果是通過 “%u” 進行轉換後再調用 long2ip,會提示錯誤:

long2ip() expects parameter 1 to be integer,string given

接着查手冊,PHP手冊上是這麼介紹的:

On 32-bit architectures, casting integer representations of IP addresses from string to integer is not suppossed to give correct results for numbers which exceed PHP_INT_MAX.

在 32 位架構中,從 string 轉換 integer 整型形式的 ip 位址将有可能導緻錯誤的結果,因為結果數字超出了 PHP_INT_MAX 限制。

最終解決辦法,封裝兩個方法:

/**
 * 代替 long2ip 函數
 * @param $ip
 * @return string
 */
function convertIpToString($ip)
{
    $long = 4294967295 - ($ip - 1);
    return long2ip(-$long);
}


/**
 * 代替 ip2long 函數
 * @param $ip
 * @return string
 */
function convertIpToLong($ip)
{
    return sprintf("%u", ip2long($ip));
}      

測試調用:

$ip_long = $this->convertIpToLong('192.168.8.30');
$long_ip = $this->convertIpToString($ip_long);

echo $ip_long; // 3232237598
echo $long_ip; // 192.168.8.30      

問題三:MySQL 中怎麼轉換 IP 位址。

  • MySQL 中提供了幾個函數,INET_ATON 将 IPV4 位址轉換為整數。 INET_NTOA 将整數轉換為 IPV4 位址。
  • 如果是 IPV6 位址也有對應的方法:INET6_ATON 和 INET6_NTOA,這兩個方法需要 5.6 以上版本才能使用。

調用示例:

SELECT INET_ATON('192.168.8.30');       // 3232237598
SELECT INET_NTOA('3232237598');         //  192.168.8.30      

問題四:PHP 中怎麼處理 IPV6 位址。

  • PHP 中沒有直接提供函數實作 IPV6 位址的轉換。 不過PHP手冊中提供了兩個方法可以實作這一需求。要運作這兩個方法首先需要開啟 php_gmp.dll 子產品。
/**
 * IPV6 位址轉換為整數
 * @param $ipv6
 * @return string
 */
function ip2long6($ipv6)
{
    $ip_n = inet_pton($ipv6);
    $bits = 15; // 16 x 8 bit = 128bit
    $ipv6long = '';
    while ($bits >= 0) {
        $bin = sprintf("%08b", (ord($ip_n[$bits])));
        $ipv6long = $bin . $ipv6long;
        $bits--;
    }
    return gmp_strval(gmp_init($ipv6long, 2), 10);
}


/**
 * 整數轉換為 IPV6 位址
 * @param $ipv6long
 * @return string
 */
function long2ip6($ipv6long)
{

    $bin = gmp_strval(gmp_init($ipv6long, 10), 2);
    if (strlen($bin) < 128) {
        $pad = 128 - strlen($bin);
        for ($i = 1; $i <= $pad; $i++) {
            $bin = "0" . $bin;
        }
    }
    $bits = 0;
    $ipv6 = '';
    while ($bits <= 7) {
        $bin_part = substr($bin, ($bits * 16), 16);
        $ipv6 .= dechex(bindec($bin_part)) . ":";
        $bits++;
    }
    // compress
    return inet_ntop(inet_pton(substr($ipv6, 0, -1)));
}      
$ip6_long = $this->ip2long6('2001:4860:a005::68');
$long_ip6 = $this->long2ip6($ip6_long);
echo $ip6_long; // 42541956150894553250710573749450571880
echo $long_ip6; // 2001:4860:a005::68