Hello, I would like to know, how to get client’s IP address using JavaScript and jQuery?any working example or code? thanks
Answered by:- jaiprakash
You can use the api ipinfo api to know the user's ip address, here is the demo code for it
$.getJSON('https://ipinfo.io/json', function(data) {
console.log(JSON.stringify(data, null, 2));
});
it gives results
{
"ip": "182.64.167.193",
"city": "New Delhi",
"region": "National Capital Territory of Delhi",
"country": "IN",
"loc": "28.6000,77.2000",
"org": "Bharti Airtel Ltd., Telemedia Services"
}
you can read more details on https://ipinfo.io/developers
Try: ipinfo.io/json
To find the client’s information from a website with SSL certificate. You can access ipify service with simple Ajax call.
$.getJSON("https://api.ipify.org?format=json", function (data) {
alert(IP: " + data.ip + ");
})
Using javascript:
window.onload = function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://api.ipify.org?format=jsonp&callback=ShowIP";
document.getElementsByTagName("head")[0].appendChild(script);
};
function ShowIP(response) {
document.getElementById("ipaddress").innerHTML = "Current IP Address is " + response.ip;
}
Answered by:- bhanu
Above answer works, but there are many more other API's which can return client's public IP using Javascript.
$.getJSON('http://ip.jsontest.com/', function(data) {
console.log(JSON.stringify(data, null, 2));
});
$.getJSON('https://api.ipregistry.co/?key=tryout', function(data) {
console.log(JSON.stringify(data, null, 2));
});
Javascript code
$.getJSON('https://api.ipify.org?format=jsonp&callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
C# code
var httpClient = new HttpClient();
var ip = await httpClient.GetStringAsync("https://api.ipify.org");
Console.WriteLine($"My public IP address is: {ip}");
$.getJSON('http://ip-api.com/json', function(data) {
console.log(JSON.stringify(data, null, 2));
});
$.getJSON('http://www.geoplugin.net/json.gp', function(data) {
console.log(JSON.stringify(data, null, 2));
});
There are many other which requires API key
You can use any of the above services, but all few limitation like No SSL support or limited free requests.
I will recommend to use https://www.ipify.org/ which is free and open source.
Answered by:- vikas_jk
If you are looking for server side solution for ip address
C#
string IPAddress = "";
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList)
{
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPAddress = Convert.ToString(IP);
}
}
Console.WriteLine(IPAddress);
https://qawithexperts.com/article/c-sharp/get-ip-address-using-c-local-and-public-ip-example/374
Java
public static String getClientIpAddress(HttpServletRequest request) {
String xForwardedForHeader = request.getHeader("X-Forwarded-For");
if (xForwardedForHeader == null) {
return request.getRemoteAddr();
} else {
// As of https://en.wikipedia.org/wiki/X-Forwarded-For
// The general format of the field is: X-Forwarded-For: client, proxy1, proxy2 ...
// we only want the client
return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
}
}
PHP
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
hope it helps.
Answered by:- DishaMandaar
You can also use
Ipstack to get details of a client's IP address using javascript or jQuery. Check out their documentation at this link https://ipstack.com/documentation
Once you have the IP address returned from https://api.ipify.org
e.g:
$.get("https://api.ipify.org?format=json", function(response) { console.log("My public IP address is: ", response.ip); });
You can get a further breakdown of its detail using Ipstack
// set endpoint and your access key
var ip = '134.201.250.155'
var access_key = 'YOUR_ACCESS_KEY';
// get the API result via jQuery.ajax
$.ajax({
url: 'https://api.ipstack.com/' + ip + '?access_key=' + access_key,
dataType: 'jsonp',
success: function(json) {
// output the "capital" object inside "location"
alert(json.location.capital);
}
});
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly