ASP code example for online people statistics

Source: Internet
Author: User

One of the most classic methods of application: Counting the number of people online, which requires the use of our global application class to count logged-in user information:

here is the code snippet:
void Application_Start (object sender, EventArgs e)
{
Code to run when the application starts
application["Count"] = 0;
System.IO.FileStream fs = System.io.file.open (Server.MapPath ("Count.txt"), system.io.filemode.openorcreate);
System.IO.StreamReader sr = new System.IO.StreamReader (FS);
application["allusers"] = Convert.ToInt32 (Sr.readline ());
Sr.close ();
Fs.close ();
}
void Application_End (object sender, EventArgs e)
{
Code to run when the application shuts down
}
void Application_Error (object sender, EventArgs e)
{
Code to run when an unhandled error occurs
}
void Session_Start (object sender, EventArgs e)
{
Code to run when a new session is started
Application.Lock ();
application["Count"] = Convert.ToInt32 (application["Count"]) + 1;
application["allusers"] = Convert.ToInt32 (application["AllUsers"]) + 1;
System.IO.FileStream fs = new System.IO.FileStream ("Count.txt", System.io.filemode.openorcreate, System.io.fileaccess.readwrite);
system.io.streamwriter SW = new System.IO.StreamWriter (FS);
Sw.writeline (application["allusers"]);
Sw.close ();
Fs.close ();
Application.UnLock ();
}
void Session_End (object sender, EventArgs e)
{
The code that runs at the end of the session.
Note: www.3ppt.com raises the Session_End event 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.
Application.Lock ();
application["Count"] = Convert.ToInt32 (application["Count"])-1;
Application.UnLock ();
}
Page just read out the content of application:
Response.Write (application["Count"]+ "Current number" + "
Total number: "+application[" allusers "]);

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.