How to find out the server IP address (using JavaS

2020-02-23 05:54发布

Is there any way you can find the IP of the server that the browser is connected to? For e.g. if the browser is accessing http://www.google.com, can we tell in any way what IP it is connected to? This is very useful in cases where Round-robin DNS is implemented. So say, the first request to a.com results in 1.1.1.1 & subsequent request result in 1.1.1.2 & so on.

I couldn't find any way to do it with JavaScript. Is it even possible? If not is there any universal way to find this info out?

标签: javascript
9条回答
ら.Afraid
2楼-- · 2020-02-23 06:19

I am sure the following code will help you to get ip address.

<script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
    }
</script>

<script type="application/javascript" src="http://www.telize.com/jsonip?callback=getip"></script>
查看更多
Root(大扎)
3楼-- · 2020-02-23 06:20

You cannot get this in general. From Javascript, you can only get the HTTP header, which may or may not have an IP address (typically only has the host name). Part of the browser's program is to abstract the TCP/IP address away and only allow you to deal with a host name.

查看更多
在下西门庆
4楼-- · 2020-02-23 06:24

Fairly certain this cannot be done. However you could use your preferred server-side language to print the server's IP to the client, and then use it however you like. For example, in PHP:

<script type="text/javascript">
    var ip = "<?php echo $_SERVER['SERVER_ADDR']; ?>";
    alert(ip);
</script>

This depends on your server's security setup though - some may block this.

查看更多
登录 后发表回答