简体   繁体   English

使用javascript的客户端IP地址

[英]Client ip address using javascript

我需要使用javascript获取客户端ip地址。实际上,我们的连接提供程序ip就是这样的122.172.127.200。我们放置了不同的私有ip,例如192.168.10.1、192.168.10.2等。我可以检索192.168.10.1 ,但是我如何使用php或javascript.SSI已检索到122.172.127.200。请帮助我。...谢谢

I'm not entirely sure whether I understand your situation, but in PHP it's easy: 我不确定我是否了解您的情况,但是在PHP中很简单:

echo $_SERVER["REMOTE_ADDR"];

in Javascript, it's not possible without some server-side interaction. 在Javascript中,没有一些服务器端交互是不可能的。 Related question here 相关问题在这里

If that doesn't address your problem, please clarify your question a bit. 如果那不能解决您的问题,请稍微澄清一下您的问题。

Update: If you are operating completely within your local network, and need to find out your public IP address provided by the ISP, you would have to either query your DSL router (or whatever device connects you to the internet) or (much much easier) this: 更新:如果您完全在本地网络中运行,并且需要找出ISP提供的公共IP地址,则必须查询DSL路由器(或将您连接到互联网的任何设备)或(要容易得多) ) 这个:

  • make an Ajax call to a PHP script outside your local network (= on the public internet) 在您的本地网络(在公共互联网上=)上对PHP脚本进行Ajax调用
  • Have that PHP script contain <? echo $_SERVER["REMOTE_ADDR"]; ?> 该PHP脚本是否包含<? echo $_SERVER["REMOTE_ADDR"]; ?> <? echo $_SERVER["REMOTE_ADDR"]; ?>
  • Your Ajax call will get back your public IP address. 您的Ajax呼叫将获得您的公共IP地址。

You need external hosting for this solution but as far as I can see, it is the easiest for that scenario. 您需要外部托管来实现此解决方案,但据我所知,对于这种情况,这是最简单的。

You can't without JavaScript on it's own. 您不能没有JavaScript本身。 But as Pekka, you can get it from any server-side script (which actually can write it into the JavaScript before it's sent to the client). 但是作为Pekka,您可以从任何服务器端脚本中获取它(实际上,可以在将其发送到客户端之前将其写入JavaScript)。 You can also get it via Ajax (xhr if it's on you domain or DSL if you find a service providing IPs in JSON format). 您也可以通过Ajax(如果位于您的域上则为xhr,如果找到以JSON格式提供IP的服务则为DSL来获得它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM