Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hello Everyone,

Can anyone help me ,

how to get client Ip Address and Machine using javascript or Jquery or any another .I have Googled so much ,But i didn't get correct results.

I have written server side code but it is giving my server details( Request.UserHostAddress & Dns.hostEntry ).

help me Please .
Posted
Updated 17-Jul-17 3:35am
Comments
Hemant Singh Rautela 7-May-13 3:03am    
Request.UserHostAddress property returns the remote User IP address, I also used it & it working fine....
Check your code once again.....
http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress.aspx
F-ES Sitecore 16-Oct-15 8:10am    
You can't get the client IP from server code or javascript. What you can get from your server code is the IP if the machine that made the request to your page, and that might or might not be the actual client. If the client uses a proxy then you'll get the address of that, and if that proxy is your server itself then the requests will appear to have come from that IP.

Request.UserHostAddress is your best bet and if it doesn't give you what you want then there isn't much you can do about that, that's just how the internet works. Google "get client ip asp.net" and you'll see this discussed already countless times.
jaket-cp 16-Oct-15 12:15pm    
more than 2 years old :)

JavaScript
<script type="text/javascript">
    window.onload = function () {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
        document.getElementsByTagName("head")[0].appendChild(script);
    };
    function DisplayIP(response) {
        document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
    }
</script>
</script>
 
Share this answer
 
Comments
Hari Chandra Prasad Ravuri 7-May-13 2:59am    
Thank you perumal i had tried this also but i didn't Provide me required output
KM Perumal 7-May-13 3:00am    
what u ve did
Hari Chandra Prasad Ravuri 7-May-13 3:09am    
<script type="text/javascript">
var IpAddress="";
window.onload = function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://jsonip.appspot.com/?callback=DisplayIP";
document.getElementsByTagName("head")[0].appendChild(script);
};
function DisplayIP(response) {
IpAddress=response.ip;
}
</script>

i have used this script but i didn't get required output
KM Perumal 7-May-13 3:17am    
Hari this method ipaddress is label id if ur create Label as ipaddress u ll get ip address
function DisplayIP(response) {
document.getElementById("ipaddress").innerHTML = "Your IP Address is " + response.ip;
}
SHARIQ_KHAN 19-Dec-14 8:18am    
can we get the client machine name using this..?
Hello,

There is no direct support for obtaining the ip address of the machine from JavaScript. But little bit server side code and AJAX you should be able to obtain the Ip address. So write a WebMethod which will retrieve the IP Address from the Request object and return it to caller. For AJAX you can use the very excellent JQuery library.

Regards,
 
Share this answer
 
Comments
Hari Chandra Prasad Ravuri 7-May-13 3:01am    
Dear prasad,
Thanks for your solution i will try ur solution
Try This.

Hope it will helpful for you.
JavaScript
string ip = Request.UserHostAddress;
string hostname = Request.UserHostName;
 
Share this answer
 
Comments
Hari Chandra Prasad Ravuri 7-May-13 5:55am    
Hi prashant
This code is giving my server Ip address and Machine Name
One use case where you might want to combine the JavaScript solution offered by KM Perumal and the web server logging suggestion of Day is to address the case where you are using layer 7 load balancing of HTTPS traffic as encrypted IP traffic (not using the load balancer to offload HTTPS decryption) to one or more backend web servers.

In this case, the web server's IP logging function will record the IP of the load balancer.

Unless your load balancer decrypts the HTTPS traffic, it cannot insert an origination (client) IP into the header (e.g. via X-Forwarded-For) to pass along to the backend web servers to record along with the remote address.

In this very specific, but not uncommon, hosting case:

1. Use JavaScript to determine the remote (client) IP

2. Insert that value in the HTTP header or as a form-submit value returned to the web server

3. Set the HTTP "Remote user" value through authentication/login (optional)

The IP address value will survive HTTPS stream load balancing to be recorded in the web server logs because the value is inserted into the HTTP response header (e.g. "%{VARNAME}^to") prior to the remote user's browser encrypting the HTTPS stream.

The web server's Remote IP-address value (e.g. Apache Module mod_log_config "%a") will still show the IP of the load balancer, but you can mine the header for the value set by your javascript.

Step three is useful when the remote user is behind a NAT network, as understand the code above, would provide the public address of the NAT router. Without a logged in user name, the IP address harvested in step 1 and returned in step 2 may represent more than one user/browser session.

In Apache mod_log-config the HTTP Header "Remote user" value can be recorded in the logs if the "%u" value is set.

Doing this, you can better get both the location and identity of the users.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900