Click here to Skip to main content
14,297,239 members
Rate this:
Please Sign up or sign in to vote.
See more:
In my project,if client sent contact details, admin need to get client Computer IP address. so i used the following code to get Client IP address.But i got wrong IP address.How can i get Client IP address after hosted to live server.
string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(clientIp))
{
    string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    clientIp = forwardedIps[forwardedIps.Length - 1];
}
else
{
    clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
Response.Write(clientIp);
Posted
Comments
Thanks7872 3-Dec-13 2:40am
   
What was the IP you got that is wrong? Is it 127.0.0.1?
Amir Mahfoozi 3-Dec-13 2:47am
   
Pay attention that testing this code in a local network will give you a different result rather than testing it on a live server.
Rate this:
Please Sign up or sign in to vote.

Solution 1

This single line of code will give you what are you looking for
string IP= Server.HtmlEncode(Request.UserHostAddress);

When you run this on localhost,it will always give you 127.0.0.1
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi...
See this link, may its help full to u.
http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress.aspx[^]
Thank u.
   

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




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