How to calculate the Website access volume in ASP. NET

Source: Internet
Author: User

The following describes how to collect statistics on website visits in ASP. NET.

1. Create a data table IPStat to store user information

The user information stored in the IPStat table only includes the IPIP_Address of the logon user, IP_Src of the IP source, and IP_DateTime of the logon time. The information in some tables is saved for only one day, if you want to calculate the monthly information, you need to save it for one month. Because I do not know much about data log operations, I am stupid to create this table.

2. obtain user information in Global. asax

The Session_Start function of Global. asax obtains relevant information when a new session is enabled. the incremental statistics of the number of online users and total number of visits are also provided here. The Code is as follows:

 
 
  1. VoidSession_Start (ObjectSender, EventArgs e)
  2.  
  3. {
  4.  
  5. // Obtain the visitor's IP address 
  6.  
  7. StringIpAddress = Request. ServerVariables ["REMOTE_ADDR"];
  8.  
  9. // Obtain the visitor's source 
  10.  
  11. StringIpSrc;
  12.  
  13. // Determine whether to navigate from the search engine 
  14.  
  15. If(Request. UrlReferrer =Null)
  16.  
  17. {
  18.  
  19. IpSrc ="";
  20.  
  21. }
  22.  
  23. Else
  24.  
  25. {
  26.  
  27. // Obtain the source address 
  28.  
  29. IpSrc = Request. UrlReferrer. ToString ();
  30.  
  31. }
  32.  
  33. // Obtain the access time 
  34.  
  35. DateTime ipDatetime = DateTime. Now;
  36.  
  37. // Save IP information to the database 
  38.  
  39. IPControl cont =NewIPControl ();
  40.  
  41. Cont. AddIP (ipAddress, ipSrc, ipDatetime );
  42.  
  43. // Obtain the page accessed by the user 
  44.  
  45. StringPageurl = Request. Url. ToString ();
  46.  
  47. // Determine whether the access is a response page 
  48.  
  49. If(Pageurl. EndsWith ("IPStat. ASPx"))
  50.  
  51. {
  52.  
  53. // Lock the variable 
  54.  
  55. Application. Lock ();
  56.  
  57. // For page access + 1 
  58.  
  59. Application ["StatCount"] =Int. Parse (Application ["StatCount"]. ToString () + 1;
  60.  
  61. // Unlock 
  62.  
  63. Application. UnLock ();
  64.  
  65. }
  66.  
  67. // Lock the variable 
  68.  
  69. Session. Timeout = 10;// Set the timeout value to 10 minutes. 
  70.  
  71. Application. Lock ();
  72.  
  73. Application ["CountSession"] = Convert. ToInt32 (Application ["CountSession"]) + 1;// Total number of visits + 1 
  74.  
  75. Application ["OnlineWhx"] = (Int) Application ["OnlineWhx"] + 1;// The number of online users plus + 1 
  76.  
  77. Session ["Login_name"] =Null;
  78.  
  79. // Unlock 
  80.  
  81. Application. UnLock ();
  82.  
  83. }
  84.  

Remind me not to forget the following code to reduce the number of online users by 1 when the user is offline.

 
 
  1. VoidSession_End (ObjectSender, EventArgs e)
  2.  
  3. {
  4.  
  5. // The code that runs when the session ends. 
  6.  
  7. // Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to InProc. 
  8.  
  9.  
  10. If the session mode is set to StateServer
  11.  
  12. // Or SQLServer, the event is not triggered. 
  13.  
  14. // Lock the variable 
  15.  
  16. Application. Lock ();
  17.  
  18. Application ["OnlineWhx"] = (Int) Application ["OnlineWhx"]-1;// Decrease the number of online users by-1 
  19.  
  20. Session ["Login_name"] =Null;
  21.  
  22. // Unlock 
  23.  
  24. Application. UnLock ();
  25.  
  26. }
  27.  

3. Save the above information to the database IPStat

ASP. the last implementation step of the website traffic statistics in. NET is to create an IPControl () class to obtain IP data, which is used to perform operations on the IPStat data of the database () class content, because it is the operation on the database in C #, to solve the SQL server database, you can understand it, it is not described here, please click this link to view.

In order to store user IP information into the database, IPControl () is called in the above Code.

 
 
  1. // Save IP information to the database 
  2.  
  3. IPControl cont =NewIPControl ();
  4.  
  5. Cont. AddIP (ipAddress, ipSrc, ipDatetime );
  6.  

The ipAddress parameter is the user IP address, ipSrc is the user source, and ipDatetime is the user entry time.

The above shows the statistics of website visits in ASP. NET.

  1. Introduction to "three-layer structure" in ASP. NET
  2. 26 methods for optimizing ASP. NET performance
  3. Comparison of html controls and web controls in ASP. NET
  4. Object Description in ASP. NET
  5. Summary of Session common problems in ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.