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:
- VoidSession_Start (ObjectSender, EventArgs e)
-
- {
-
- // Obtain the visitor's IP address
-
- StringIpAddress = Request. ServerVariables ["REMOTE_ADDR"];
-
- // Obtain the visitor's source
-
- StringIpSrc;
-
- // Determine whether to navigate from the search engine
-
- If(Request. UrlReferrer =Null)
-
- {
-
- IpSrc ="";
-
- }
-
- Else
-
- {
-
- // Obtain the source address
-
- IpSrc = Request. UrlReferrer. ToString ();
-
- }
-
- // Obtain the access time
-
- DateTime ipDatetime = DateTime. Now;
-
- // Save IP information to the database
-
- IPControl cont =NewIPControl ();
-
- Cont. AddIP (ipAddress, ipSrc, ipDatetime );
-
- // Obtain the page accessed by the user
-
- StringPageurl = Request. Url. ToString ();
-
- // Determine whether the access is a response page
-
- If(Pageurl. EndsWith ("IPStat. ASPx"))
-
- {
-
- // Lock the variable
-
- Application. Lock ();
-
- // For page access + 1
-
- Application ["StatCount"] =Int. Parse (Application ["StatCount"]. ToString () + 1;
-
- // Unlock
-
- Application. UnLock ();
-
- }
-
- // Lock the variable
-
- Session. Timeout = 10;// Set the timeout value to 10 minutes.
-
- Application. Lock ();
-
- Application ["CountSession"] = Convert. ToInt32 (Application ["CountSession"]) + 1;// Total number of visits + 1
-
- Application ["OnlineWhx"] = (Int) Application ["OnlineWhx"] + 1;// The number of online users plus + 1
-
- Session ["Login_name"] =Null;
-
- // Unlock
-
- Application. UnLock ();
-
- }
-
Remind me not to forget the following code to reduce the number of online users by 1 when the user is offline.
- VoidSession_End (ObjectSender, EventArgs e)
-
- {
-
- // The code that runs when the session ends.
-
- // Note: The Session_End event is triggered only when the sessionstate mode in the Web. config file is set to InProc.
-
-
- If the session mode is set to StateServer
-
- // Or SQLServer, the event is not triggered.
-
- // Lock the variable
-
- Application. Lock ();
-
- Application ["OnlineWhx"] = (Int) Application ["OnlineWhx"]-1;// Decrease the number of online users by-1
-
- Session ["Login_name"] =Null;
-
- // Unlock
-
- Application. UnLock ();
-
- }
-
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.
- // Save IP information to the database
-
- IPControl cont =NewIPControl ();
-
- Cont. AddIP (ipAddress, ipSrc, ipDatetime );
-
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.
- Introduction to "three-layer structure" in ASP. NET
- 26 methods for optimizing ASP. NET performance
- Comparison of html controls and web controls in ASP. NET
- Object Description in ASP. NET
- Summary of Session common problems in ASP. NET