views:

208

answers:

4

I need get the IP address for the user cross the proxy using PHP... any suggestion ?

+2  A: 

Not exactly a dupe, but you'll find what you need in this SO question.

KB22
+5  A: 

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
A: 

i don't about php but. in .net code like this Request.UserHostAddress try this

Surya sasidhar
+1  A: 

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