Having issue with storing ip adress in MySQL db table
Getting users ip adress with this piece of code. I have unsigned int field for ip's in my db table
<?php
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//proxy check
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
$ip = ip2long($ip);
?>
Testing website in local server. The problem is, getting '63177408abbad957ed13ae1a2dd3e0b3ed47bd0b48ffcb362' after ip2long conversion. Tried
$ip=$_SERVER['REMOTE_ADDR'];
$ip = ip2lon开发者_如何学JAVAg($ip);
too. Still no success. How can i fix it?
You're most likely on a 32bit install of PHP, which is corrupting the IP address. Try to do the conversion inside mysql, which doesn't suffer the problem:
$ip = mysql_real_escape_string('127.0.0.1');
$sql = "INSERT INTO yourtable (ip) VALUES (INET_ATON('$ip'));";
The other alternative is to force PHP to treat it as an unsigned integer, as per the Notes on the ip2long man page: $ip = sprintf('%u', ip2long($ip));
As well, remember that IPv6 will be widespread "any day now", and you'll need to use a 128bit integer to store those values (which MySQL doesn't support - it'd have to be two 64bit bigints at least).
精彩评论