使用以下PHP / SQL ...
function ip2long6($ip)
{//ipv6 to ipv4 (string) to get Windows 7 local working
// Temp work-around until we convert the string to support ipv6 natively in SQL.
if (substr_count($ip, '::')) {$ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip,':')).':',$ip);}
$ip = explode(':',$ip);
$r_ip = '';
foreach ($ip as $v) {$r_ip .= str_pad(base_convert($v,16,2),16,0,STR_PAD_LEFT);}
return base_convert($r_ip,2,10);
}
$ip = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$ip = mysql_real_escape_string(ip2long6($ip));
INSERT INTO log (ip) VALUES (INET_ATON('1208321'))
我收到以下错误...
列'ip'不能为空
我们现在怎么能让$ ip兼容IPv4?
工作答案......
if (is_int($ip)) {$ipsql = mysql_real_escape_string("INET_ATON('$ip')");}
else {$ipsql = mysql_real_escape_string($ip);}
有了这个,我只需回复INET_ATON('$ip')
$ipsql
。
答案 0 :(得分:0)
您正在使用INET_ATON转换stringed-int。该函数需要一个dotted.quad地址,例如
INET_ATON('127.0.0.1');
不是有效输入,因此该函数返回null:
mysql> select inet_aton('12345'), inet_aton('127.0.0.1');
+--------------------+------------------------+
| inet_aton('12345') | inet_aton('127.0.0.1') |
+--------------------+------------------------+
| NULL | 2130706433 |
+--------------------+------------------------+