我在一些帖子中读到,IP地址可以转换并保存为mysqldb中的整数。有人能举例说明吗?
提前谢谢。
ip2long()
:http://php.net/manual/en/function.ip2long.php
ip2long()
http://php.net/manual/en/function.ip2long.php
既然你说你想在MySQL数据库中使用它,那么就用这两个函数来转换与MySQL的INET_ATON和INET_NTOA兼容的数字。
<?php
function convertIpToString($ip)
{
$long = 4294967295 - ($ip - 1);
return long2ip(-$long);
}
function convertIpToLong($ip)
{
return sprintf("%u", ip2long($ip));
}
?>
参考MySQL函数:
INET_ATON() -- Return the numeric value of an IP address
INET_NTOA() -- Return the IP address from a numeric value
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton
ip2long $ip = ip2long($ip);
也检查这个链接
您可以使用ip2long()转换为int
$Db_ip=ip2long("127.0.0.1");
获取ip地址
$ip = long2ip($Db_ip); // "127.0.0.1"