Click here to Skip to main content
12,078,259 members (49,572 online)
Rate this:
 
Please Sign up or sign in to vote.
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 6-May-13 21:37pm
Comments
hemantrautela 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.
Member 10454138 16-Oct-15 12:15pm
   
more than 2 years old :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

<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>
  Permalink  
Comments
   
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
   
<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..?
Member 10230348 18-Jun-15 3:01am
   
how to store ip address in database using jquery can u explain me im new to jquery
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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,
  Permalink  
Comments
   
Dear prasad,
Thanks for your solution i will try ur solution
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try This.

Hope it will helpful for you.
string ip = Request.UserHostAddress;
string hostname = Request.UserHostName;
  Permalink  
Comments
   
Hi prashant
This code is giving my server Ip address and Machine Name
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 6 Nov 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100