javascript 如何使用Javascript获取客户端的IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4670615/
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
How to get the IP address of Client Using Javascript
提问by sreekanth
Hi Please let me know how can i get the client IP address. I have used the following .
嗨,请让我知道如何获取客户端 IP 地址。我使用了以下内容。
Ip = <--#echo var="REMOTE_ADDR"-->;
ip = '<%= Request.UserHostAddress>';
But these are not working.
但这些都行不通。
采纳答案by Nathan
Most web servers (I'm assuming you're using IIS) provide this information in the REMOTE_ADDR Environment Variable.
大多数 Web 服务器(我假设您使用的是 IIS)在 REMOTE_ADDR环境变量中提供此信息。
Your samples are trying to get at the server's variables with Classic ASP and Server Side Includes, both of which are turned off by default on modern IIS web servers. You may need to enable classic ASP or SSI, or use the ServerVariables property using ASP.NET
您的示例试图通过 Classic ASP 和 Server Side Includes 获取服务器的变量,这两个在现代 IIS Web 服务器上默认关闭。您可能需要启用经典 ASP 或 SSI,或使用ASP.NET使用ServerVariables 属性
回答by Usman Malik
Use the following code for to get the IP Address in ASP Classic
使用以下代码获取 ASP Classic 中的 IP 地址
<%
Dim UserIPAddress
UserIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If UserIPAddress = "" Then
UserIPAddress = Request.ServerVariables("REMOTE_ADDR")
End If
%>
回答by Shadow Wizard is Ear For You
Try this:
试试这个:
<script type="text/javascript">
var ip = "<%=Request.ServerVariables("REMOTE_ADDR")%>";
alert(ip);
</script>
Assuming this is .asppage you should see alert with your ip address.
假设这是.asp页面,您应该会看到带有您的 IP 地址的警报。
回答by Ivo Wetzel
You cannot do this with JavaScript alone, it looks to me like you're trying to use server side code on the client, that doesn't work.
你不能单独用 JavaScript 做到这一点,在我看来,你试图在客户端上使用服务器端代码,这是行不通的。
The easiest, fastest (and ugliest) of doing this would be to output the IP on the s*erver side*, into a script tag on the page that's being send to the user.
最简单、最快(也是最丑陋)的做法是将服务器端的 IP 输出到发送给用户的页面上的脚本标记中。
No idea which language you're using on the server side, so I'll provide you some pseudo code.
不知道您在服务器端使用哪种语言,所以我会为您提供一些伪代码。
echo '<script type="text/javascript">var USER_IP = ' + getRemoteAddess() + ';</script>'
This will introduce a global variable called USER_IPinto the page, make sure that the code that uses this variable comes afterthe above script tag.
这将引入一个名为全局变量USER_IP到页面中,请确保使用此变量的代码来后,上面的脚本标签。
回答by J. Scott Elblein
For the record in case anyone is looking for the PHP solution, you'd just do it like this:
为了记录以防万一有人正在寻找 PHP 解决方案,您只需这样做:
<?php echo $_SERVER['REMOTE_ADDR']; ?>
For example, if you were passing it in a JS array, it would look like this:
例如,如果你在一个 JS 数组中传递它,它看起来像这样:
tr._setSomeVar(5, "Remote Address", "<?php echo $_SERVER['REMOTE_ADDR']; ?>", 9);
回答by artwl
try
尝试
ip = '<%= Request.UserHostAddress %>';
回答by Anax
You cannot get the IP using pure Javascript. The two solutions you provided require server-side scripting.
您无法使用纯 Javascript 获取 IP。您提供的两个解决方案需要服务器端脚本。

