雖然說現在 32-bit 的機器會越來越少,不過踩到這個 bug 還是記錄一下

在官方文件中有提到 http://php.net/manual/en/function.ip2long.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.
如果你是使用 32-bit 的作業系統,有可能出來的值會是負數,例如 210.61.150.2 這個 Hinet IP

由於現在開發系統的時候通常在資料庫的設計上都會把登入 IP 設為 INT (10) unsigned
如果出現負數的情況時就是 500 Internal Server Error 噴給你看

所以解法就是再包一層 sprintf 讓他確保是正數
sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']))
arrow
arrow

    ieon 發表在 痞客邦 留言(0) 人氣()