How do I display the number of online users and their locations ?? [Transferred from author: Zi Yang]

Source: Internet
Author: User
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.

My implementation principle is: Every time I access the aspx file, I will judge whether there is this user in the online table (the logged user name has been logged on, and the recorded IP Address has not been logged on). If it 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:
Whether the length of the primary key field type is null
1 uson_serial int 4 0 No.
0 uson_user varchar 20 0 User Name (IP address if logon is not performed)
0 uson_company varchar 100 0 company name ('tourist 'if not logged in ')
0 uson_ip varchar 20 0 IP Address
0 uson_date datetime8 0 last operation time
0 uson_url varchar 100 0 path of the last operation page

IV,Program
Note:
1. The program is located in global. asax.
2. I am using Forms authentication
3. Please 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 ();
}

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.