ASP implements a solution to count current online users

Source: Internet
Author: User

A solution for counting current online users

When I had a problem with an online communication website, I had a headache, that is, the issue of real-time online user statistics. Customer requirements: count the current number of online users, number of visitors, number of members, and list of online users, including visitors, members, and administrators. (If the visitor is a visitor, the ID of the visitor is automatically generated. If the visitor is a member, the member name is displayed ). Because it requires real-time performance, I will first pass out the ideas solved with global. Asa.

The key to the problem is how to determine whether the user has left, and how to execute a file or function when the user leaves.

After discussion with some friends on the Internet, the problem was finally solved.

The solution is to compile a general page. The so-called general page is that every page in the application contains this page, for example: header. ASP. on this page, use XMLHTTP to write a SectionCodeThis code sends a request to the server every 10 or 20 seconds to update the online time of the current user and delete the user whose online time exceeds a certain time, keep online user records in the database in real time.

The main implementation method is:

Create a new database with the following field names: ID (character), name (character), user (number) TT (date), admin (permission code, 0-common user, 1-Administrator)

Table Name: Online

Header. asp Certificate

========================================================== ================================

<%

......

If SESSION ("s_in") <> 1 and Session ("s_name") = "" then' if the user logs on for the first time

Rs. Open "select * from online", Conn, 3,3
Rs. addnew
RS ("ID") = session. sessionid
RS ("name") = "Tourist" & session. sessionid
RS ("user") = 0' 0 indicates that the user has not logged in. It is a tourist identity.
RS ("TT") = now
Rs. Update
Rs. Close
Session ("s_in") = 1' sets that the user's data has been stored in the database, indicating that the user has been online
End if

If SESSION ("s_name") <> "" then' if the user has logged on through the logon box
Rs. Open "select * from online where id = '" & session. sessionid & "'", Conn, 3,3
RS ("name") = SESSION ("s_name ")
RS ("admin") = SESSION ("s_admin") 'updates the user name to the member name.
RS ("user") = 1' indicates that the user has logged in and is a member.
RS ("TT") = now set the current system time to the user's logon time
Rs. Update
Rs. Close
End if

......

%>

......

<Head>

......

<Script language = JavaScript>
Function Test ()
{
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp ");
XMLHTTP. Open ("Post", "onceonline. asp", false); // send an update request to onceonline. asp.
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send ();
}
Setinterval ("test ();", 10); // send an update request in 10 seconds
</SCRIPT>

......

</Head>

......

========================================================== ============================

Onceonline. asp

<%
Rs. Open "select TT from online where id = '" & session. sessionid & "'", Conn, 3,3
RS ("TT") = now () 'updates the online time of the Current online user
Rs. Update
Rs. Close

Rs. Open "delete from online where datediff ('s ', TT, now ()> 60", Conn, 3, 1' users with deletion timeout
%>

========================================================== ======================================

In this way, the real-time user list in the database is basically guaranteed, and the error depends on the difference between the Update Time and the deletion time and the server processing speed, we recommend that you do not set the time interval for deleting expired users too small, which may lead to errors of 0 online users.

This scheme is successfully debugged on win2000 + SQL Server2000. As this scheme has great requirements on the system, we hope other friends can come up with a better solution to solve this problem together!

Related Article

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.