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 IP address (IP_Address), IP source (IP_Src), and logon time (IP_DateTime) of the logon user ), some table information is saved for only one day. If you want to count the information of each month, 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:
Void Session_Start (object sender, EventArgs e ){
// Obtain the visitor's IP address
String ipAddress = Request. ServerVariables ["REMOTE_ADDR"];
// Obtain the visitor's source
String ipSrc;
// 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 = new IPControl ();
Cont. AddIP (ipAddress, ipSrc, ipDatetime );
// Obtain the page accessed by the user
String pageurl = 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.
Void Session_End (object sender, EventArgs e ){
// The code that runs when the session ends.
// Note: only when the sessionstate mode in the Web. config file is set to InProc,
The Session_End event is triggered.
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 = new IPControl ();
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.