我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。

然而,我并不反对使用免费的第三方脚本/服务。


当前回答

好吧,我偏离了这个问题,但我今天有一个类似的需求,尽管我无法使用Javascript从客户端找到ID,但我做了以下工作。

服务器端:-

<div style="display:none;visibility:hidden" id="uip"><%= Request.UserHostAddress %></div>

使用Javascript

var ip = $get("uip").innerHTML;

我使用ASP。Net Ajax,但您可以使用getElementById而不是$get()。

发生的事情是,我在页面上有一个隐藏的div元素,用户的IP从服务器呈现。而在Javascript中,我只是加载这个值。

这可能对像你这样有类似需求的人(比如我,但我还没有弄清楚)有帮助。

干杯!

其他回答

有一种更简单和免费的方法,不需要访问者的任何许可。

它包括向http://freegeoip.net/json提交一个非常简单的Ajax POST请求。一旦收到JSON格式的位置信息,您就可以通过更新页面或重定向到新页面来做出相应的反应。

以下是提交位置信息请求的方法:

jQuery.ajax( { 
  url: '//freegeoip.net/json/', 
  type: 'POST', 
  dataType: 'jsonp',
  success: function(location) {
     console.log(location)
  }
} );

我会说乍得和马耳他有很好的答案。然而,他们的想法很复杂。所以我建议这个代码,我发现从广告国家插件

<script>
<script language="javascript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="javascript">
mmjsCountryCode = geoip_country_code();
mmjsCountryName = geoip_country_name();

</script>

没有ajax。只是简单的javascript。: D

如果你访问http://j.maxmind.com/app/geoip.js,你会看到它包含

function geoip_country_code() { return 'ID'; }
function geoip_country_name() { return 'Indonesia'; }
function geoip_city()         { return 'Jakarta'; }
function geoip_region()       { return '04'; }
function geoip_region_name()  { return 'Jakarta Raya'; }
function geoip_latitude()     { return '-6.1744'; }
function geoip_longitude()    { return '106.8294'; }
function geoip_postal_code()  { return ''; }
function geoip_area_code()    { return ''; }
function geoip_metro_code()   { return ''; }

它还没有真正回答这个问题,因为

http://j.maxmind.com/app/geoip.js不包含IP(尽管我打赌它使用IP来获取国家)。

但是编写这样的PhP脚本非常容易

function visitorsIP()   { return '123.123.123.123'; }

让。打开http://yourdomain.com/yourip.php。

然后做

<script language="javascript" src="http://yourdomain.com/yourip.php"></script>

问题中特别提到不能使用第三方脚本。别无他法。Javascript无法知道你的IP。但是其他可以通过javascript访问的服务器也可以毫无问题地工作。

获取系统本地IP:

  try {
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
    var rtc = new RTCPeerConnection({ iceServers: [] });
    if (1 || window.mozRTCPeerConnection) {
        rtc.createDataChannel('', { reliable: false });
    };

    rtc.onicecandidate = function (evt) {
        if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
    };
    rtc.createOffer(function (offerDesc) {
        grepSDP(offerDesc.sdp);
        rtc.setLocalDescription(offerDesc);
    }, function (e) { console.warn("offer failed", e); });


    var addrs = Object.create(null);
    addrs["0.0.0.0"] = false;
    function updateDisplay(newAddr) {
        if (newAddr in addrs) return;
        else addrs[newAddr] = true;
        var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
        LgIpDynAdd = displayAddrs.join(" or perhaps ") || "n/a";
        alert(LgIpDynAdd)
    }

    function grepSDP(sdp) {
        var hosts = [];
        sdp.split('\r\n').forEach(function (line) {
            if (~line.indexOf("a=candidate")) {
                var parts = line.split(' '),
                    addr = parts[4],
                    type = parts[7];
                if (type === 'host') updateDisplay(addr);
            } else if (~line.indexOf("c=")) {
                var parts = line.split(' '),
                    addr = parts[2];
                alert(addr);
            }
        });
    }
})();} catch (ex) { }

实际上没有可靠的方法来获取客户端计算机的IP地址。

这涉及到一些可能性。如果用户有多个接口,那么使用Java的代码将会中断。

http://nanoagent.blogspot.com/2006/09/how-to-find-evaluate-remoteaddrclients.html

从这里的其他答案来看,听起来您可能想要获得客户端的公共IP地址,这可能是他们用来连接到互联网的路由器的地址。很多其他的答案都谈到了这个。我建议创建并托管您自己的服务器端页面,用于接收请求并使用IP地址响应,而不是依赖于其他人的服务,因为其他人的服务可能继续工作,也可能无法继续工作。

你不能。你得问服务员。