I. Principles
The Application_AuthenticateRequest event and Application_BeginRequest event in global. asax in. net are triggered every time the aspx file is accessed. However, in Application_BeginRequest, ticket tickets with FROMS authentication cannot be identified. Therefore, it can only be put in Application_AuthenticateRequest.
The implementation principle is: each time you access the aspx file, you will determine whether there is a logged user name in the online table and no recorded IP address). If not, the user's identity, last access time, last access IP address, and last access URL are stored in the database. If the database already exists, update the record to update the last access time, IP address, and last access URL.
At the same time, it is time-out to delete data in the database that is more than 20 minutes away from the current time ).
Ii. Advantages
In this way, you can not only see the exact number of people online, but also know who is online and whether to log on to the website, the proportion of the number of people who are already members, and the location where they are, calculate the number of people on a page.
Iii. Database Structure:
Field |
Type |
Length |
Description |
1uson_serial |
Int |
40 |
Serial number |
0uson_user |
Varchar |
200 |
If the user name is not logged in, it is an IP address) |
0uson_company |
Varchar |
1000 |
If the company name is not logged in, it is 'tourists ') |
0uson_ip |
Varchar |
200 |
IP address |
0uson_date |
Datetime |
80 |
Last operation time |
0uson_url |
Varchar |
1000 |
Last operation page path |
4. Procedures
Note:
1. The program is located in global. asax.
2. FORMS authentication
3. Use System. Web. Security
Protected void Application_AuthenticateRequest (Object sender, EventArgs e)
{
String strUserID = string. Empty;
String strCompany = string. Empty;
If (Request. IsAuthenticated)
{
FormsIdentity identity = (FormsIdentity) User. Identity;
FormsAuthenticationTicket ticket = identity. Ticket;
StrUserID = User. Identity. Name;
StrCompany = ticket. UserData. Split ("|". ToCharArray () [2];
}
Else
{
StrUserID = Request. UserHostAddress;
StrCompany = "Tourists ";
}
MemberOnlineInfo objOnline = new MemberOnlineInfo (strUserID, Request. UserHostAddress, DateTime. Now. ToString (), Request. FilePath, strCompany );
MemberAccount account = new MemberAccount ();
If (! Account. CheckUserOnline (strUserID ))
Account. AddOnline (objOnline );
Else
Account. UpdateOnline (objOnline );
// Delete the expired Member
Account. DeleteOnline ();
}