I need get the IP address for the user cross the proxy using PHP... any suggestion ?
Asked
Active
Viewed 437 times
1
-
1Surely you could google this? – Davie Nov 05 '09 at 10:48
-
6Give him the answer instead of stupid comments like that. – Filip Ekberg Nov 05 '09 at 10:56
-
This question has numerous duplicates on SO. – Pekka Nov 05 '09 at 11:50
-
4@Pekka: Don't just tell us that there are duplicates without telling some of. – Moayad Mardini Nov 05 '09 at 15:40
2 Answers
5
Usually the proxy adds the specific header X-Forwarded-For
that you can check in PHP via the server variable $_SERVER['HTTP_X_FORWARDED_FOR']

Steve Schnepp
- 4,620
- 5
- 39
- 54
1
It's a hard thing to do, and not all proxies will provide the real IP of the user. I wrote this function before which attempts to find the most likely IP of the user though:
function get_ip_address() {
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
elseif (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_COMING_FROM']))
return $_SERVER['HTTP_COMING_FROM'];
elseif (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_X_COMING_FROM']))
return $_SERVER['HTTP_X_COMING_FROM'];
elseif (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
elseif (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
elseif (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
elseif (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR'];
else return $_SERVER['REMOTE_ADDR'];
}
That function just returns 1 IP if you are just storing IP in database or something, but I have a similar function which gives a text based response with all of the possible IPs found.
function get_txt_ip_address() {
$return = '';
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_CLIENT_IP']))
$return .= $_SERVER['HTTP_CLIENT_IP']." (client) / ";
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_COMING_FROM']))
$return .= $_SERVER['HTTP_COMING_FROM']." (cf) / ";
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_X_COMING_FROM']))
$return .= $_SERVER['HTTP_X_COMING_FROM']." (xcf) / ";
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_FORWARDED']))
$return .= $_SERVER['HTTP_FORWARDED']." (fw) / ";
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_X_FORWARDED']))
$return .= $_SERVER['HTTP_X_FORWARDED']." (xfw) / ";
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_FORWARDED_FOR']))
$return .= $_SERVER['HTTP_FORWARDED_FOR']." (fwf) / ";
if (ereg("([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})",$_SERVER['HTTP_X_FORWARDED_FOR']))
$return .= $_SERVER['HTTP_X_FORWARDED_FOR']." (xfwf) / ";
$return .= $_SERVER['REMOTE_ADDR']." (ra) / ";
$return .= gethostbyaddr($_SERVER['REMOTE_ADDR']);
return $return;
}

Tim
- 812
- 2
- 11
- 22