javascript 通过第三方 Web 服务获取客户端 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17414972/
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
Get client IP address via third party web service
提问by Milton90
I would like to read my ip address from the following page(http://l2.io/ipor other) using javascript to save him in my variable "myIp".
我想从以下页面(http://l2.io/ip或其他)读取我的 ip 地址,使用 javascript 将他保存在我的变量“myIp”中。
function getMyIP() {
var myIp;
...
return myIp;
}
How can you do?
你能怎么办?
回答by Fabrício Matté
Checking your linked site, you may include a script tag passing a ?var=desiredVarName
parameter which will be set as a global variable containing the IP address:
检查您的链接站点,您可能包含一个传递?var=desiredVarName
参数的脚本标记,该参数将被设置为包含 IP 地址的全局变量:
<script type="text/javascript" src="http://l2.io/ip.js?var=myip"></script>
<!-- ^^^^ -->
<script>alert(myip);</script>
I believe I don't have to say that this can be easily spoofed (through either use of proxies or spoofed request headers), but it is worth noting in any case.
我相信我不必说这很容易被欺骗(通过使用代理或欺骗请求标头),但无论如何都值得注意。
HTTPS support
HTTPS 支持
In case your page is served using the https
protocol, most browsers will block content in the same page served using the http
protocol (that includes scripts and images), so the options are rather limited. If you have < 5k hits/day, the Smart IP APIcan be used. For instance:
如果您的页面使用该https
协议提供服务,大多数浏览器将阻止使用该http
协议提供服务的同一页面中的内容(包括脚本和图像),因此选项相当有限。如果您每天的点击次数小于 5k,则可以使用Smart IP API。例如:
<script>
var myip;
function ip_callback(o) {
myip = o.host;
}
</script>
<script src="https://smart-ip.net/geoip-json?callback=ip_callback"></script>
<script>alert(myip);</script>
Edit: Apparently, this https
service's certificate has expired so the user would have to add an exception manually. Open its API directly to check the certificate state: https://smart-ip.net/geoip-json
编辑:显然,此https
服务的证书已过期,因此用户必须手动添加例外。直接打开其API查看证书状态:https: //smart-ip.net/geoip-json
With back-end logic
有后台逻辑
The most resilient and simple way, in case you have back-end server logic, would be to simply output the requester's IP inside a <script>
tag, this way you don't need to rely on external resources. For example:
如果您有后端服务器逻辑,最有弹性和最简单的方法是简单地在<script>
标签内输出请求者的 IP ,这样您就不需要依赖外部资源。例如:
PHP:
PHP:
<script>var myip = '<?php echo $_SERVER['REMOTE_ADDR']; ?>';</script>
There's also a more sturdy PHP solution (accounting for headers that are sometimes set by proxies) in this related answer.
在这个相关的答案中还有一个更强大的 PHP 解决方案(考虑到有时由代理设置的标头)。
C#:
C#:
<script>var myip = '<%= Request.UserHostAddress %>';</script>
回答by Ranjit Kumar
$.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
alert(location.ip);
}
});
This will work https too
这也适用于 https
回答by Dayson
A more reliable REST endpoint would be http://freegeoip.net/json/
更可靠的 REST 端点是http://freegeoip.net/json/
Returns the ip address along with the geo-location too. Also has cross-domain requests enabled (Access-Control-Allow-Origin: *) so you don't have to code around JSONP.
也返回 IP 地址和地理位置。还启用了跨域请求(Access-Control-Allow-Origin:*),因此您不必围绕 JSONP 进行编码。
回答by Soumya Kanti
If you face an issue of CORS, you can use https://api.ipify.org/.
如果您遇到 CORS 问题,可以使用https://api.ipify.org/。
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
publicIp = httpGet("https://api.ipify.org/");
alert("Public IP: " + publicIp);
I agree that using synchronous HTTP call is not good idea. You can use async ajax call then.
我同意使用同步 HTTP 调用不是一个好主意。然后您可以使用异步 ajax 调用。
回答by bd_person
<script type="application/javascript">
function getip(json){
alert(json.ip); // alerts the ip address
}
</script>
<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>
回答by John Williams
This pulls back client info as well.
这也会拉回客户端信息。
var get = function(u){
var x = new XMLHttpRequest;
x.open('GET', u, false);
x.send();
return x.responseText;
}
JSON.parse(get('http://ifconfig.me/all.json'))
回答by Kenny Stier
Well, if in the HTML you import a script...
好吧,如果在 HTML 中导入脚本...
<script type="text/javascript" src="//stier.linuxfaq.org/ip.php"></script>
You can then use the variable userIP (which would be the visitor's IP address) anywhere on the page.
然后,您可以在页面上的任何位置使用变量 userIP(这将是访问者的 IP 地址)。
To redirect: <script>if (userIP == "555.555.555.55") {window.location.replace("http://192.168.1.3/flex-start/examples/navbar-fixed-top/");}</script>
重定向: <script>if (userIP == "555.555.555.55") {window.location.replace("http://192.168.1.3/flex-start/examples/navbar-fixed-top/");}</script>
Or to show it on the page: document.write (userIP);
或者在页面上显示: document.write (userIP);
DISCLAIMER: I am the author of the script I said to import. The script comes up with the IP by using PHP. The source code of the script is below.
免责声明:我是我说要导入的脚本的作者。该脚本使用 PHP 提供 IP。该脚本的源代码如下。
<?php
//Gets the IP address
$ip = getenv("REMOTE_ADDR") ;
Echo "var userIP = '" . $ip . "';";
?>
<?php
//Gets the IP address
$ip = getenv("REMOTE_ADDR") ;
Echo "var userIP = '" . $ip . "';";
?>