這里主要介紹一下 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