javascript 如何获取客户端/用户IP地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16414702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:33:55  来源:igfitidea点击:

How to get client / user ip address?

javascriptip-addressload-balancingclientip

提问by RajeshKumar.K

Is it possible to get the ip address of the user (visitor of my website) without using server side languagelike PHP?

是否可以在不使用PHP 等服务器端语言的情况下获取用户(我网站的访问者)的 IP 地址?

I have a problem while using the server language (PHP) because of load balancer for my server. I tried http_x_forwared_forthat is also not retrieving the perfect ip address.

由于我的服务器的负载平衡器,我在使用服务器语言 (PHP) 时遇到问题。我试过http_x_forwared_for也没有检索到完美的 IP 地址。

采纳答案by Paul Rivera

Try this:

试试这个:

$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
   alert(data.host);
});

回答by Paul Rivera

function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
    }

IN PHP :)