开发者

I need get the IP address

I need get the IP address for 开发者_运维问答the user cross the proxy using PHP... any suggestion ?


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']


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;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜