How to find out the server IP address (using JavaScript) that the browser is connected to?

前端 未结 10 2155
说谎
说谎 2020-12-29 04:20

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

相关标签:
10条回答
  • 2020-12-29 04:51

    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>
    
    0 讨论(0)
  • 2020-12-29 04:54

    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.

    0 讨论(0)
  • 2020-12-29 04:56

    I believe John's answer is correct. For instance, I'm using my laptop through a wifi service run by a conference centre -- I'm pretty sure that there is no way for javascript running within my browser to discover the IP address being used by the service provider. On the other hand, it may be possible to address a suitable external resource from javascript. You can write your own if your own by making an ajax call to a server which can take the IP address from the HTTP headers and return it, or try googling "find my ip". The cleanest solution is probably to capture the information before the page is served and insert it in the html returned to the user. See How to get a viewer's IP address with python? for info on how to capture the information if you are serving the page with python.

    0 讨论(0)
  • 2020-12-29 04:56

    <script type="application/javascript">
      function getIP(json) {
        document.write("My public IP address is: ", json.ip);
      }
    </script>
    <script type="application/javascript" src="http://ipinfo.io/?format=jsonp&callback=getIP"></script>

    0 讨论(0)
提交回复
热议问题