A simple statistical method of website traffic in asp.net

Source: Internet
Author: User
Tags object datetime implement sql net string variable tostring
A QQ friend asked me how personal website traffic statistics, because I am a beginner to do the site, the level of limited, the method introduced may be stupid method, but on my website can make do with the use, I hope we have a lot of advice.

A QQ friend asked me how personal website traffic statistics, because I am a beginner to do the site, the level of limited, the method introduced may be stupid method, but on my website can make do with the use, I hope we have a lot of advice.

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

Obtain information about The Global.asax session _start, which 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: Session_End events are 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 number minus 1

session["login_name"] = null;

Unlock

Application.UnLock ();

}

Third, save the above information to the database Ipstat

Created a class Ipcontrol () to obtain IP data information, which is used to implement the operation of IPSTAT data on the database, the content of the Ipcontrol () class, because it is the operation of the database in C # to solve the SQL Server database, you can understand it, here is not introduced, 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. Chinaitlab



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.