javascript 如何使用Javascript代码查找客户端的IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16412162/
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 find client's IP Address using Javascript code
提问by Annie
I want to find client's IP Address when accessing my app..How to find using javascript code..kindly provide help with sample code..I tried wit tis code but m not getting
我想在访问我的应用程序时找到客户端的 IP 地址。
function getip(json){ alert(json.ip); // alerts the ip address }
函数 getip(json){ 警报(json.ip); // 提醒 IP 地址 }
回答by gopi1410
You can't using ONLY JavaScript on it's own. You need to use a server side script ans call it using Ajax to perform this task.
你不能只使用 JavaScript。您需要使用服务器端脚本并使用 Ajax 调用它来执行此任务。
For example in PHP script, you can do the following to get the client's ip address:
例如在 PHP 脚本中,您可以执行以下操作来获取客户端的 ip 地址:
echo $_SERVER["REMOTE_ADDR"];
Next call this script using Ajax and display/use it in your page.
接下来使用 Ajax 调用此脚本并在您的页面中显示/使用它。
回答by David M?rtensson
At least there used to be a way using Java to do this, but Javascript alone cannot find a machines IP.
至少曾经有一种方法可以使用 Java 来执行此操作,但是仅靠 Javascript 无法找到机器 IP。