For a website, it is very important to count the number of online users. At ordinary times, we also find that many websites and forums are displayed with online users. If a website has a large number of online users, it is amazing for users to see such a number. Since I only knew about this knowledge point and did not know it before, I had a chance to see it again in the past two days. So I sorted out a simple version myself, it is also convenient for you to give comments.
This article mainly uses Application and Session to count the number of online users globally. Currently, only verified logon users are counted. The general implementation scheme is as follows:
1. When the global Application is started, Application ["count"] = 0; the number of online users in Initialization statistics is 0.
2. process a simple Form logon authentication and accumulate 1 on the global variable Application ["count"] during logon.
3. When triggered when the Session fails, the user logs out, or the browser is closed, the global Session function is used to perform a 1-plus operation on the global variable Application ["count.
Step 1: Create an Asp. Net MVC3.0 Web project and select the Razor engine. Set the initial number of online users. Find Application_Start in the Global. asax file.
] = ;
Step 2: simply modify the logon verification, authenticate successfully, write the Session value, and Add 1 to the number of online users.
ActionResult LogOn(LogOnModel model, (ModelState.IsValid) System.Web.HttpContext.Current.Session.Add(= System.Web.HttpContext.Current.Application[] = Convert.ToInt32(System.Web.HttpContext.Current.Application[]) + (Request.QueryString[] != RedirectToAction(,
Session_End(] = Convert.ToInt32(Application[]) -
In this case, remember to lock the processing because there may be concurrency issues. The lock and unlock operations should also be performed during logon.
Step 4: process the logout Action as follows:
RedirectToAction(,
Cancel the current session to trigger the global Session_End function defined above.
Step 5: add the onbeforeunload event to the body tag of the layout template.
Because this event is called during refresh and shutdown, you can perform the following operations during implementation:
<script type=> n = window..screenX - b = n > document.documentElement.scrollWidth - (b && window..clientY < || window. ( == = </script>
In this case, the call Action is as follows:
}
The instance download link is http://pan.baidu.com/share/link? Consumer id = 1839967473 & uk = 4244870074. You can download the project and run it for testing. For now, I have basically implemented the IE8 test function. The issue of compatibility with various browsers is not taken into consideration for the triggering of browser web pages. If you are interested in this issue, please try IE first. If any problem is found, it is expected to be promptly notified to make changes to the solution.