To realize the statistics of website traffic in asp.net

Source: Internet
Author: User
Tags datetime implement sql net variable tostring access
To achieve the statistics of site traffic in asp.net. Here's how to make a statistic about the amount of web site traffic in asp.net.

First, the establishment of a data table Ipstat for the storage of user information

The user information I hold in the Ipstat table includes only the IP (ip_address) of the logged-on user, the IP source (ip_src) and the login time (ip_datetime), the information of some tables I only keep one day's information, if the information to be counted each month will be kept for one months. Because I do not understand the operation of the data log, so create this table, so say I stupid, haha.

Second, in the Global.asax to obtain the user information

Get the information when the Global.asax Session_Start is enabled for a new session, with the number of online people and the incremental statistics for the total number of visitors, 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 the visitor

String ipsrc;

To determine if the search engine has navigated

if (Request.urlreferrer = = null) {

IPSRC = "";

}

else {

Get Source Address

IPSRC = Request.UrlReferrer.ToString ();

}

Get access Time

DateTime ipdatetime = DateTime.Now;

Save 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 ();

Determines whether the access is a default page

if (Pageurl. EndsWith ("ipstat.aspx")) {

Lock variable

Application.Lock ();

+1 for page traffic

application["Statcount"] = Int. Parse (application["Statcount"). ToString ()) + 1;

Unlock

Application.UnLock ();

}

Lock variable

Session.Timeout = 10;

Set timeout of 10 minutes

Application.Lock ();

application["countsession"] = Convert.ToInt32 (application["countsession"]) + 1;

Total number of visits +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 make the number of people online minus 1 when the user is offline.


void Session_End (object sender, EventArgs e) {

Code that runs at the end of the session.

Note: Only when the sessionstate mode in the Web.config file is set to InProc,

The Session_End event is raised.

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 number minus 1

session["login_name"] = null;

Unlock

Application.UnLock ();

}

Third, save the above information to the database Ipstat


Asp. NET Web site traffic statistics, the last step is to create a class Ipcontrol () to obtain IP data information, to implement the operation of the database Ipstat data, about the Ipcontrol () class content, because it is in C # in the operation of the database, to solve the SQL Server database, you can understand it, this is not introduced here, please click the link to view.


In order to implement the user IP information into the database, in the above code to Ipcontrol () to call


Save 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 access time.

The above implementation of the ASP.net Web site traffic statistics.



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.