2016-09-07 78 views
0

我有一個簡單的Web應用程序,我需要跟蹤訪問者。每個訪問者的信息存儲在如下表所示:ASP.NET Web應用訪問者計數器

VisitorsDetails:

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

[ComputerName]是位訪問者的IP地址。

表中包含ipv6記錄,這些記錄在計算訪問者數量時導致問題,或者有時會導致令牌錯誤。

強制訪問者使用ipv4是否明智? 如果是的話,怎麼樣?

回答

0
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; 
} 
} 

請參考以下鏈接也:

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

+0

謝謝,這正是我一直在尋找的。 –

0
void Application_Start(object sender, EventArgs e) 
{ 
    // Code that runs on application startup 
    Application["NoOfVisitors"] = 0; 
} 





void Session_Start(object sender, EventArgs e) 
{ 
    // Code that runs when a new session is started 
    Application.Lock(); 
    Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1; 
    Application.UnLock(); 
} 

in .aspx page 

<asp:Label runat="server" ID="lbluser" /> 
in .aspx.cs 

protected void Page_Load(object sender, EventArgs e) 
{ 
    lbluser.Text = Application["NoOfVisitors"].ToString(); 
} 
+0

不錯,但主要的問題支持IPv6 –

0
protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RegisterRoutes(RouteTable.Routes); 

     WebpageCounter.SaveVisitor(new WebpageVisitor() 
     { 
      VisitorIP = HttpContext.Current.Request.UserHostAddress, 
      VisitedOn = DateTime.Now 
     }); 
    } 
------------- 
Below Code is for IP Address 
string ipaddress; 
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 
if (ipaddress == "" || ipaddress == null) 
    ipaddress = Request.ServerVariables["REMOTE_ADDR"]; 
+0

這可能是HTTP_X_FORWARDED_FOR返回多個值。我如何確保我只會得到ipv4 –

相關問題