Soroosh Sharif Soroosh Sharif - 3 months ago 25
ASP.NET (C#) Question

ASP.NET web application visitor counter

I've got a simple web application which I need to keep track of visitors. Each visitor's information is stored in the following table:

VisitorsDetails:

| ID | ComputerName | VisitDate | CountryCode | Platform | Browser | BrowserVersion |


[ComputerName]
is vistor's IP address.

The table contains ipv6 records which causes problems in calculating visitor's count or sometimes causes Token errors.

Is it wise to force visitors to use ipv4?
if yes, how?

Answer
    public string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}

foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
 { 
if (IPA.AddressFamily.ToString() == "InterNetwork")
 { 
IP4Address = IPA.ToString(); 
break;
 } 
}

Please refer below links also:

http://www.4guysfromrolla.com/articles/071807-1.aspx http://tutorialgenius.blogspot.in/2010/09/aspnet-get-ipv4-address-even-if-user-is.html