Javascript 可以知道你的 IP 吗?
Possible Duplicate:
Get Client IP using just Javascript?
I know PhP can know your IP from <?php echo $_SERVER['REMOTE_ADDR']; ?>
How can javascript do so?
Or is it true that because javascript is client side it doesn't know the IP of the computer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果包含 JS 代码的文件是由 PHP 解析的,那么您应该能够通过 PHP 将 IP 地址注入到 JS 代码中。
If the file containing your JS code is parsed by PHP, you should be able to inject the IP-address into the JS code by PHP.
JavaScript 在您的浏览器中执行,它无法知道您的 IP。
但是你可以通过AJAX从后端获取IP
JavaScript is executed in your browser, it can't know your IP.
But you can get IP from back-end with AJAX
这是正确的。如果不连接到另一台服务器,则无法确认您的公共 IP,因此它不能直接在客户端 JavaScript 中使用。
That's correct. There's no way to confirm your public IP without connecting to another server, so it's not directly available in client-side JavaScript.
您可以使用 HTTP 请求来调用提供 IP 的现有 API。例如,domaintools.com 上有 XML API,但应该有访问起来更加简单的 API。
You can use an HTTP request to call an existing API which provides the IP. For example there is the XML API at domaintools.com, but there should be much more simpler APIs to access.