简体   繁体   中英

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:

echo $_SERVER["REMOTE_ADDR"];

in Javascript, it's not possible without some server-side interaction. 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:

  • make an Ajax call to a PHP script outside your local network (= on the public internet)
  • Have that PHP script contain <? echo $_SERVER["REMOTE_ADDR"]; ?> <? echo $_SERVER["REMOTE_ADDR"]; ?>
  • Your Ajax call will get back your public IP address.

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. 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). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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