我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
当前回答
Javascript / jQuery获取客户端的IP地址和位置(国家,城市)
你只需要嵌入一个带有“src”链接的标签到服务器。服务器将以Object / JSON的形式返回“codehelper_ip”,您可以立即使用它。
// First, embed this script in your head or at bottom of the page.
<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
// You can use it
<script language="Javascript">
alert(codehelper_ip.IP);
alert(codehelper_ip.Country);
</script>
更多信息在Javascript检测真实IP地址加国家
如果你正在使用jQUery,你可以尝试:
console.log(codehelper_ip);
它将显示关于返回对象的更多信息。
如果你想要回调函数,请试试这个:
// First, embed this script in your head or at bottom of the page.
<script language="Javascript" src="http://www.codehelper.io/api/ips/?callback=yourcallback"></script>
// You can use it
<script language="Javascript">
function yourcallback(json) {
alert(json.IP);
}
</script>
其他回答
如果你总是包含一个文件,你可以做一个简单的ajax get:
function ip_callback() {
$.get("ajax.getIp.php",function(data){ return data; }
}
ajax。getip。php是这样的
<?=$_SERVER['REMOTE_ADDR']?>
使用Smart-IP.net Geo-IP API。例如,使用jQuery:
$(document).ready( function() {
$.getJSON( "http://smart-ip.net/geoip-json?callback=?",
function(data){
alert( data.host);
}
);
});
试试这个:http://httpbin.org/ip(或https://httpbin.org/ip)
https的示例:
$.getJSON('https://httpbin.org/ip', function(data) {
console.log(data['origin']);
});
来源:http://httpbin.org/
这里的大多数答案都“绕过”服务器端代码的需要……攻击别人的服务器。这是一种完全有效的技术,除非您确实需要在不访问服务器的情况下获得IP地址。
传统上,如果没有某种插件,这是不可能的(即使这样,如果你在NAT路由器后面,你可能会得到错误的IP地址),但随着WebRTC的出现,实际上是有可能做到这一点…如果你的目标浏览器支持WebRTC(目前:Firefox, Chrome和Opera)。
请阅读mido的回答,了解如何使用WebRTC检索有用的客户端IP地址。
对这个问题有两种解释。大多数人将“客户端IP”解释为Web服务器在局域网外和Internet上看到的公共IP地址。不过,在大多数情况下,这不是客户端计算机的IP地址
我需要运行我的JavaScript软件的浏览器的计算机的真实IP地址(它几乎总是LAN上NAT层后面的本地IP地址)。
Mido发布了一个奇妙的答案,上面,这似乎是唯一的答案,真正提供了客户端的IP地址。
谢谢你,Mido!
但是,给出的函数是异步运行的。我需要在代码中实际使用IP地址,使用异步解决方案时,我可能会尝试在检索/学习/存储IP地址之前使用IP地址。在使用它们之前,我必须能够等待结果的到来。
下面是Mido函数的“可等待”版本。我希望它能帮助到其他人:
function findIP(onNewIP) { // onNewIp - your listener function for new IPs var promise = new Promise(function (resolve, reject) { try { var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome var pc = new myPeerConnection({ iceServers: [] }), noop = function () { }, localIPs = {}, ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g, key; function ipIterate(ip) { if (!localIPs[ip]) onNewIP(ip); localIPs[ip] = true; } pc.createDataChannel(""); //create a bogus data channel pc.createOffer(function (sdp) { sdp.sdp.split('\n').forEach(function (line) { if (line.indexOf('candidate') < 0) return; line.match(ipRegex).forEach(ipIterate); }); pc.setLocalDescription(sdp, noop, noop); }, noop); // create offer and set local description pc.onicecandidate = function (ice) { //listen for candidate events if (ice && ice.candidate && ice.candidate.candidate && ice.candidate.candidate.match(ipRegex)) { ice.candidate.candidate.match(ipRegex).forEach(ipIterate); } resolve("FindIPsDone"); return; }; } catch (ex) { reject(Error(ex)); } });// New Promise(...{ ... }); return promise; }; //This is the callback that gets run for each IP address found function foundNewIP(ip) { if (typeof window.ipAddress === 'undefined') { window.ipAddress = ip; } else { window.ipAddress += " - " + ip; } } //This is How to use the Waitable findIP function, and react to the //results arriving var ipWaitObject = findIP(foundNewIP); // Puts found IP(s) in window.ipAddress ipWaitObject.then( function (result) { alert ("IP(s) Found. Result: '" + result + "'. You can use them now: " + window.ipAddress) }, function (err) { alert ("IP(s) NOT Found. FAILED! " + err) } ); <h1>Demo "Waitable" Client IP Retrieval using WebRTC </h1>