Website access statistics in ASP.net

Source: Internet
Author: User

A QQ friend asked me how my personal website visits were counted. As I was a beginner at the website, the level was limited. The method described may be stupid, however, I hope you will give me more advice on how to use it on my website.

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

InGlobal. asaxSession_Start is used to obtain the relevant information when the new session is enabled. At the same time, the incremental statistics of the number of online users and total number of visits are implemented 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; // 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: 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

An IPControl () class is created to obtain IP data.IPStatData Operations, about the IPControl () class, because it is a database operation in C # To solve the SQL server database, you can understand it, here is not introduced, click this link to view details.

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.

4. Create a timer and regularly operate related data

ForIPStaFor database data, you need to create one or more timers, count the traffic for one day in the 10 seconds before the 24 hours every night, delete them, and save the statistical results to another data table, for the page to show yesterday's access volume is called. To create and use a timer, click Create one or more timers for your reference.

Please criticize and correct the above mistakes. Thank you!
 

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.