How to achieve the statistics of website traffic in ASP.

Source: Internet
Author: User
Tags get ip

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:

  1. void Session_Start (object sender, EventArgs e)
  2. {
  3. Get the IP of the visitor
  4. String ipAddress = request.servervariables["REMOTE_ADDR"];
  5. Get the source of your visitors
  6. String ipsrc;
  7. To determine whether to navigate from a search engine.
  8. if (request.urlreferrer = = null)
  9. {
  10. IPSRC = "";
  11. }
  12. Else
  13. {
  14. Get Source Address
  15. IPSRC = Request.UrlReferrer.ToString ();
  16. }
  17. Get access Time
  18. DateTime ipdatetime = DateTime.Now;
  19. To save the IP information to the database
  20. Ipcontrol cont = new Ipcontrol ();
  21. Cont. Addip (ipAddress, IPSRC, ipdatetime);
  22. Get the page that the user visited
  23. String pageurl = Request.Url.ToString ();
  24. Determine if the access is the default page
  25. if (Pageurl. EndsWith ("ipstat.aspx"))
  26. {
  27. Lock variable
  28. Application.Lock ();
  29. Number of page visits +1
  30. application["Statcount"] = int. Parse (application["Statcount").   ToString ()) + 1;
  31. Unlock
  32. Application.UnLock ();
  33. }
  34. Lock variable
  35. Session.Timeout = 10; //Set timeout to 10 minutes
  36. Application.Lock ();
  37. application["countsession"] = Convert.ToInt32 (application["countsession"]) + 1; Total number of Visitors +1
  38. application["onlinewhx"] = (int) application["ONLINEWHX"] + 1; //Online number plus +1
  39. session["login_name"] = null;
  40. Unlock
  41. Application.UnLock ();
  42. }

As a reminder, don't forget the following code to realize the number of people online minus 1 when the user is offline.

  1. void Session_End (object sender, EventArgs e)
  2. {
  3. The code that runs at the end of the session.
  4. Note: The Session_End event is raised only if the sessionstate mode in the Web. config file is set to InProc.
  5. If the session mode is set to StateServer
  6. or SQL Server, the event is not raised.
  7. Lock variable
  8. Application.Lock ();
  9. application["onlinewhx"] = (int) application["Onlinewhx"]-1; //Online people minus 1
  10. session["login_name"] = null;
  11. Unlock
  12. Application.UnLock ();
  13. }

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

    1. To save the IP information to the database
    2. Ipcontrol cont = new Ipcontrol ();
    3. 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.

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.