BrMe BrMe - 3 months ago 105
Javascript Question

Get local IP address of client using javascript


Possible Duplicate:

Get Client IP using just Javascript?




How can I retrieve/get the client IP address?

Answer

If you need server side scripting, i mean Java code, then refer this

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class IPAddress{
    public static void main(String[] a) {

        try {
            InetAddress thisIp = InetAddress.getLocalHost();
            System.out.print(thisIp.getHostAddress());     
        } catch (UnknownHostException ex) {
            Logger.getLogger(study.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

UPDATE

As you said, you need in javaScript. Please refer below code and let me know.

<html>
<head>
<script type="text/javascript" language="javascript">
  function myIP() {
    if (window.XMLHttpRequest) 
   xmlhttp = new XMLHttpRequest();
    else 
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.open("GET","http://jsonip.appspot.com/",false);
    xmlhttp.send();

    hostipInfo = xmlhttp.responseText;
    obj = JSON.parse(hostipInfo);
    document.getElementById("IP").value=obj.ip;
    document.getElementById("ADDRESS").value=obj.address;
}
</script>
</head>
<body onload="myIP()">
IP :<input type="text" id="IP" name="IP" />
ADDRESS :<input type="text" id="ADDRESS" name="ADDRESS" />

</body>
</html>

You may also refer How to display client IP Address.

Comments