Get local IP address of client using javascript
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.
How to get client / user ip address?
Try this:
$.getJSON("http://smart-ip.net/geoip-json?callback=?", function(data){
alert(data.host);
});
How to determine a user's IP address in node
In your request
object there is a property called socket
, which is a net.Socket
object. The net.Socket
object has a property remoteAddress
, therefore you should be able to get the IP with this call:
request.socket.remoteAddress
(if your node version is below 13, use the deprecated now request.connection.remoteAddress
)
EDIT
As @juand points out in the comments, the correct method to get the remote IP, if the server is behind a proxy, is request.headers['x-forwarded-for']
EDIT 2
When using express
with Node.js:
If you set app.set('trust proxy', true)
, req.ip
will return the real IP address even if behind proxy. Check the documentation for further information
JavaScript to get Client IP Address bypassing Browser Extensions
You can use public services which are not blacklisted by ad blockers, like https://www.myexternalip.com/json but eventually it can also be blacklisted, it's not under your control.
The most reliable way to reduce the risk of using a service that can be blacklisted would be building your own server as suggested in descriptions or keeping an updated list of alternative services in case of failed requests. Probably you won't get rid of your "problem" without spending some money.
Related Topics
How to Remove a Property from a JavaScript Object
How to Pad a Value With Leading Zeros
Get All Non-Unique Values (I.E.: Duplicate/More Than One Occurrence) in an Array
React Setstate Not Updating State
How to Check If an Object Is an Array
Use Basic Authentication With Jquery and Ajax
Calculate Age Given the Birth Date in the Format Yyyymmdd
What Are the Precise Semantics of Block-Level Functions in Es6
How to Prevent a Parent'S Onclick Event from Firing When a Child Anchor Is Clicked
How to Initialize a JavaScript Date to a Particular Time Zone
Short Circuit Array.Foreach Like Calling Break
How to Read a Local Text File in the Browser
How to Completely Uninstall Node.Js, and Reinstall from Beginning (Mac Os X)
How to Check If an Object Has a Specific Property in JavaScript
Chrome/Firefox Console.Log Always Appends a Line Saying 'Undefined'