您可能需要考虑将 IP 转换为数字。查找速度快一点,因为它是数字数据,您可以在查询中使用 INET_ATON() 和 INET_NTOA()。
http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_inet-aton
mysql> SELECT INET_ATON('10.0.5.9');
-> 167773449
http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_inet-ntoa
mysql> SELECT INET_NTOA(167773449);
-> '10.0.5.9'
PHP转换为数字
$ipA = $_SERVER["REMOTE_ADDR"];
$octets = split ("\.", $ipA);
$ipN = ($octets[3] + $octets[2] * 256 + $octets[1] * pow(256,2) + $octets[0] * pow(256,3);
此外,您可能需要考虑与此功能一起使用的 IP 地址:
/* Get Actual IP Address, in spite of proxy server */
function getRealIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{ $ip=$_SERVER['HTTP_CLIENT_IP']; }
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{ $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; }
else
{ $ip=$_SERVER['REMOTE_ADDR']; }
return $ip;
}
重新编辑 IPv6:
原则仍然适用,但 IPv6 Conversions 已经在How to convert IPv6 from binary for storage in MySQL 中得到解答