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 principle is: each time you access the aspx file, the system checks whether the user exists in the online table (the logged user name, the IP address of the logged-on record). If the user does not exist, 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, delete the data in the database that is more than 20 minutes away from the current time (if no operation is performed for 20 minutes, the operation times out ).
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 No.
0uson_user varchar 200 user name (IP address if logon fails)
0uson_company varchar 1000 company name ('tourist 'if not logged in ')
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 ();
}