How to achieve the statistics of Web site traffic in ASP 2009-07-30 15:50 Anonymous Web-wing tutorial net font size:T | T
This article describes how to make statistics on the amount of Web site visits in ASP.
Ad:51cto Net + 12th salon: The beauty of big data-how to drive user experience with data
Here's how to do statistics on the amount of Web site visits in ASP.
First, establish a data table Ipstat for storing user information
The user information I store in the Ipstat table only includes the logged-on user's IP (ip_address), IP source (IP_SRC) and logon time (Ip_datetime), the information of the tables I only save one day of information, if you want to count the information for each month to be kept for one months. Because I don't quite understand the operation of the data log, so I created this table, so I am stupid, haha.
Ii. Obtaining user information in Global.asax
Get information about Global.asax's Session_Start, when new session is enabled, and the number of people who are online and the total number of visitors, the code is as follows:
- void Session_Start (object sender, EventArgs e)
- {
- Get the IP of the visitor
- String ipAddress = request.servervariables["REMOTE_ADDR"];
- Get the source of your visitors
- String ipsrc;
- To determine whether to navigate from a search engine.
- if (request.urlreferrer = = null)
- {
- IPSRC = "";
- }
- Else
- {
- Get Source Address
- IPSRC = Request.UrlReferrer.ToString ();
- }
- Get access Time
- DateTime ipdatetime = DateTime.Now;
- To save the IP information to the database
- Ipcontrol cont = new Ipcontrol ();
- Cont. Addip (ipAddress, IPSRC, ipdatetime);
- Get the page that the user visited
- String pageurl = Request.Url.ToString ();
- Determine if the access is the default page
- if (Pageurl. EndsWith ("ipstat.aspx"))
- {
- Lock variable
- Application.Lock ();
- Number of page visits +1
- application["Statcount"] = int. Parse (application["Statcount"). ToString ()) + 1;
- Unlock
- Application.UnLock ();
- }
- Lock variable
- Session.Timeout = 10; //Set timeout to 10 minutes
- Application.Lock ();
- application["countsession"] = Convert.ToInt32 (application["countsession"]) + 1; Total number of Visitors +1
- application["onlinewhx"] = (int) application["ONLINEWHX"] + 1; //Online number plus +1
- session["login_name"] = null;
- Unlock
- Application.UnLock ();
- }
As a reminder, don't forget the following code to realize the number of people online minus 1 when the user is offline.
- void Session_End (object sender, EventArgs e)
- {
- The code that runs at the end of the session.
- Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to InProc.
- If the session mode is set to StateServer
- or SQL Server, the event is not raised.
- Lock variable
- Application.Lock ();
- application["onlinewhx"] = (int) application["Onlinewhx"]-1; //Online people minus 1
- session["login_name"] = null;
- Unlock
- Application.UnLock ();
- }
Third, save the above information to the database Ipstat
Asp. NET site traffic statistics in the last implementation step, is to create a Get IP data information class Ipcontrol (), used to implement the database Ipstat data operations, about the Ipcontrol () class content, because it is C # in the operation of the database to solve the SQL Server database, you can read it, this is not introduced here, please click on the link to view.
In order to implement the user IP information into the database, Ipcontrol () is called in the code above
- To save the IP information to the database
- Ipcontrol cont = new Ipcontrol ();
- Cont. Addip (ipAddress, IPSRC, ipdatetime);
Parameter ipaddress for user ip,ipsrc as user source, Ipdatetime for user to enter time.
This enables the statistics of website visits in ASP.
How to achieve the statistics of website traffic in ASP.