Asp.net online user count statistics, page access Application, session

Source: Internet
Author: User

1. Create a website and add several forms. WebForm1.aspx, ViewStateForm. aspx

2. Add the Global application class "Global. aspx" under the root directory of the website ". (Important)

3. There is an inherent format and session information structure in "Global. aspx.

4. Add the processing code to each function in "Global. aspx. The details are as follows:

<% @ Application Language = "C #" %>

<Script runat = "server">


Void Application_Start (object sender, EventArgs e) // initialize the number of online users of the site
{
// Code that runs when the application starts // initialization variables: UserCount and StatCount

Application. Lock (); // critical variable. The Lock function is used and cannot be accessed by other users.
Application ["UserCount"] = 0;
Application. UnLock (); // The critical variable is unlocked.

Application. Lock (); // critical variable. The Lock function is used and cannot be accessed by other users.
Application ["StatCount"] = 0;
Application. UnLock (); // The critical variable is unlocked.

Application. Lock (); // critical variable. The Lock function is used and cannot be accessed by other users.
Application ["StatCount_ViewSF"] = 0;
Application. UnLock (); // The critical variable is unlocked.

}

Void Application_End (object sender, EventArgs e)
{
// Code that runs when the application is closed

}

Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

Void Session_Start (object sender, EventArgs e) // Add one to the number of online users
{
// The code that runs when the new session starts
Application. Lock (); // critical variable. The Lock function is used and cannot be accessed by other users.
Application ["UserCount"] = Int32.Parse (Application ["UserCount"]. ToString () + 1;
Application. UnLock (); // The critical variable is unlocked.

// Test the access volume on a page ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ ※※※※※※※※※※※
String pageurl = Request. Url. ToString (); // obtain the page accessed by the user

If (pageurl. EndsWith ("WebForm1.aspx") // checks whether the access is a response page.
{
// Lock the variable
Application. Lock ();
// Add one page access
Application ["StatCount"] = int. Parse (Application ["StatCount"]. ToString () + 1;
// Unlock
Application. UnLock ();
}

Else if (pageurl. EndsWith ("ViewStateForm. aspx") // determines whether the access is a response page.
{
// Lock the variable
Application. Lock ();
// Add one page access
Application ["StatCount_ViewSF"] = int. Parse (Application ["StatCount_ViewSF"]. ToString () + 1;
// Unlock
Application. UnLock ();
}

} // & **************** $ $

Void Session_End (object sender, EventArgs e) // The number of online users on the site is reduced by one.
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session mode is set to StateServer
// Or SQLServer, the event is not triggered.
Application. Lock ();
Application ["UserCount"] = Int32.Parse (Application ["UserCount"]. ToString ()-1;
Application. UnLock ();

}

// Processing events at the beginning and end of an Http request
Protected void Application_BeginRequest (object sender, EventArgs e)
{
// Obtain the TabID of the table
// Int tabId = 0; int tabIndex = 0;
// If (Request. Params ["TabId"]! = Null)
//{
// TabId = Int32.Parse (Request. Params ["TabId"]);

//}
// If (Request. Params ["tabIndex"]! = Null)
//{
// TabIndex = Int32.Parse (Request. Params ["TabIndex"]);
//}

}
Protected void Application_EndRequest (object sender, EventArgs e)
{

}

// Handle Http request verification events
Protected void Application_AuthenticateRequest (object sender, EventArgs e)
{

}


</Script>

5. Add the following code to the corresponding CS file of webForm1.aspx:

Public partial class WebForm1: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack) {OutputUserCount ();}
}
Protected void OutputUserCount () // display the number of online users of the current site
{
Response. Write ("number of online users :");
Response. Write (Application ["UserCount"]. ToString ());
Response. Write ("person. ");

Response. Write ("Access volume on this page :");
Response. Write (Application ["StatCount"]. ToString ());
Response. Write (". ");

}
}

6. Add the following code to the corresponding CS file of ViewStateForm. aspx:

Public partial class ViewStateForm: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{


If (! Page. IsPostBack) {OutputUserCount ();}
}
Protected void OutputUserCount () // display the number of online users of the current site
{
Response. Write ("number of online users :");
Response. Write (Application ["UserCount"]. ToString ());
Response. Write ("person. ");

Response. Write ("Access volume on this page :");
Response. Write (Application ["StatCount_ViewSF"]. ToString ());
Response. Write (". ");
}

}

7. Some session configuration controls are also available in webconfig.

<SessionState mode = "InProc"
Cookieless = "true"
Timeout = "20"/>

<! --
Session status settings
By default, asp.net uses cookies to indicate which requests belong to a specific session.
If the cookie is unavailable, you can trace the session by adding the session identifier to the url.
To disable cookie, set sessionstate cookieless = "true ".
Used for the first time: <sessionState
Mode = "InProc"
StateConnectionString = "tcpip = 127.0.0.1: 42424"
SqlConnectionString = "data source = 127.0.0.1; userid = sa; password ="
Cookieless = "false"
Timeout = "20"
/>. I don't know why? A new website can be created without any use. It seems that this is not the reason. It should be a temporary system error.
-->

Then you can test it in IIS. After IIS is restarted, this processing method starts from scratch for statistics.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/eqiang8271/archive/2007/10/23/1838742.aspx

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.