Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
friends,
i have used the following code for checking ip address but it is not giving exact ip address
---------------------------------------------------------------------------------------------
C#
public string IpAddress()
      {
          string strIpAddress;
          strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
          if (strIpAddress == null)
          {
              strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
          }
          return strIpAddress;
      }
      protected void btnipcheck_Click(object sender, EventArgs e)
      {
          string str;
          try
          {
               str = IpAddress();
               Session["ip"] = str;
               Label1.Text = Session["ip"].ToString();
          }
          catch
          {
              Label1.Text = Session["ip"].ToString();
          }
      }

------------------------------------------------------------------------------------------
i have tested on local host
it is giving o/p 127.0.0.1
but my ip address is different :10.25.---.---
---------------------------------------------------------------------------------
so what is the other code for getting the exact ip address of client machine.
Posted

Of course it's giving you 127.0.0.1! You're testing it on your DEV machine! The request doesn't go out to the internet and back in so it's not going to use the 10.x.x.x route.

If you want the IP the request came from, you'll have to run the site on a seperate machine. Then you can make a request from your machine and get the real IP address.
 
Share this answer
 
Comments
maan_k 15-Feb-13 10:02am    
does it means my code is correct.will it work fine when the site hosted?
Dave Kreskowiak 15-Feb-13 12:46pm    
He's a wacked idea: TEST IT AND SEE FOR YOURSELF!
Logi Guna 16-Feb-13 2:02am    
strIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
that's working for me to get client IP.
of course you must host your site and browse from another machine.
My 5 for Dave.
I use this in a Windows EXE program:
string strHostName = System.Net.Dns.GetHostName();
string strIPAddress = "";
System.Net.IPAddress[] objAddressList = System.Net.Dns.GetHostEntry(strHostName).AddressList;
for (x = 0; x <= objAddressList.GetUpperBound(0); x++) 
{
    if (objAddressList[x].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
    {
        strIPAddress = objAddressList[x].ToString();
        break; 
    }
}




I found this via Google Search at get ip address in asp page - client side[^]
Response.Write("Local Machine Host name is "+Dns.GetHostName()+"<br>");
         string hostname = Dns.GetHostName();
         IPHostEntry ipEntry = Dns.GetHostByName(hostname);
         IPAddress[] addr = ipEntry.AddressList;
         for (int i = 0; i < addr.Length; i++)
           {
               Response.Write(string.Format("IP Address {0}: {1} ", i, addr[i].ToString()) + "<br>");
           }


And some good tips at A Gotcha Identifying the User's IP Address[^]

 
Share this answer
 
v4

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