2013-03-31 24 views
0

用下面的PHP/SQL ...PHP的Windows 7的IPv6變通IP列不能爲空

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

您正在使用INET_ATON轉換一個絃樂-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 | 
+--------------------+------------------------+ 
+0

一個疏忽,謝謝...現在在尋找它... – John