I have a a bit of code in my PHP which tries to get the user's IP address:
// Test if it is a shared client
if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
$userIP = $_SERVER['HTTP_CLIENT_IP'];
}
// Is it a proxy address?
elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$userIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$userIP = $_SERVER['REMOTE_ADDR'];
}
Then I store $userIP
in a MySQL table using the INET_ATON()
function to convert the string to an integer. I can retrieve this value using MySQL's INET_NTOA()
function.
It works perfectly most of the time. However, sometimes I get an IP like 0.0.7.102
, for example.
I know these functions don't work with IPv6, but could this problem be caused by something else? Are there a lot of people using IPv6 already? How do I deal with this?
Thanks!