我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
我需要以某种方式检索客户端的IP地址使用JavaScript;没有服务器端代码,甚至没有SSI。
然而,我并不反对使用免费的第三方脚本/服务。
当前回答
你不能。你得问服务员。
其他回答
首先是实际的答案:不可能仅使用客户端执行的代码来找出您自己的IP地址。
但是,您可以对https://hutils.loxal.net/whois执行GET请求,并接收类似这样的内容以获取客户端的IP地址
{
"ip": "88.217.152.15",
"city": "Munich",
"isp": "M-net Telekommunikations GmbH",
"country": "Germany",
"countryIso": "DE",
"postalCode": "80469",
"subdivisionIso": "BY",
"timeZone": "Europe/Berlin",
"cityGeonameId": 2867714,
"countryGeonameId": 2921044,
"subdivisionGeonameId": 2951839,
"ispId": 8767,
"latitude": 48.1299,
"longitude": 11.5732,
"fingerprint": "61c5880ee234d66bded68be14c0f44236f024cc12efb6db56e4031795f5dc4c4",
"session": "69c2c032a88fcd5e9d02d0dd6a5080e27d5aafc374a06e51a86fec101508dfd3",
"fraud": 0.024,
"tor": false
}
Appspot.com回调服务不可用。ipinfo。IO似乎在工作。
我做了一个额外的步骤,检索所有的地理信息使用AngularJS。(感谢里卡多)来看看。
<div ng-controller="geoCtrl">
<p ng-bind="ip"></p>
<p ng-bind="hostname"></p>
<p ng-bind="loc"></p>
<p ng-bind="org"></p>
<p ng-bind="city"></p>
<p ng-bind="region"></p>
<p ng-bind="country"></p>
<p ng-bind="phone"></p>
</div>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
<script src="http://code.angularjs.org/1.2.12/angular-route.min.js"></script>
<script>
'use strict';
var geo = angular.module('geo', [])
.controller('geoCtrl', ['$scope', '$http', function($scope, $http) {
$http.jsonp('http://ipinfo.io/?callback=JSON_CALLBACK')
.success(function(data) {
$scope.ip = data.ip;
$scope.hostname = data.hostname;
$scope.loc = data.loc; //Latitude and Longitude
$scope.org = data.org; //organization
$scope.city = data.city;
$scope.region = data.region; //state
$scope.country = data.country;
$scope.phone = data.phone; //city area code
});
}]);
</script>
工作页面在这里:http://www.orangecountyseomarketing.com/projects/_ip_angularjs.html
对这个问题有两种解释。大多数人将“客户端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>
我将提供一个方法,当我想在html页面中存储信息时,我经常使用这个方法,并且希望我的javascript能够读取信息,而不需要实际地将参数传递给javascript。当您的脚本被外部引用而不是内联引用时,这尤其有用。
然而,它不符合“没有服务器端脚本”的标准。但是如果你可以在你的html中包含服务器端脚本,可以这样做:
将隐藏标签元素设置在html页面的底部,就在结束主体标签的上方。
你的标签看起来是这样的:
<label id="ip" class="hiddenlabel"><?php echo $_SERVER['REMOTE_ADDR']; ?></label>
一定要创建一个名为hiddenlabel的类,并设置visibility:hidden,这样就没有人真正看到这个标签。你可以用这种方式在隐藏标签中存储很多东西。
现在,在你的javascript中,检索存储在标签中的信息(在这种情况下是客户端的ip地址),你可以这样做:
var ip = document.getElementById("ip").innerHTML;
现在你的变量“ip”等于ip地址。现在可以将ip传递给API请求了。
*编辑2年后* 两个小的改进:
我经常使用这种方法,但是调用标签class="data",因为实际上,这是一种存储数据的方法。类名“hiddenlabel”是一个愚蠢的名字。
第二个修改是在样式表中,而不是visibility:hidden:
.data{
display:none;
}
...是更好的方法。
试试这个:http://httpbin.org/ip(或https://httpbin.org/ip)
https的示例:
$.getJSON('https://httpbin.org/ip', function(data) {
console.log(data['origin']);
});
来源:http://httpbin.org/