A solution that counts current online users

Source: Internet
Author: User
Tags header table name time interval
Solution | statistics | Online when doing an online communication site, there's a problem that's really troubling me about real-time statistics on online users, customer requirements: Statistics of current online numbers, number of visitors, number of members, list of online users, including visitors, members and administrators (if it is a tourist, automatically generate the visitor ID, If it is a member, show the name of the member. Because it requires real-time, first I will use the idea of Global.asa solution pass away.

The crux of the problem is how to judge the user has left, and how to execute a file or a function when the user leaves.

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

The principle of the solution is: to write a common page, the so-called common page, that is, every page in the application contains this page, For example: header.asp, on this page, write a piece of code with XMLHTTP, the function of this code is to send a request every 10 seconds or 20 seconds to the server, the purpose is to update the current user's online time and delete online time more than a certain time of users, so that the database of online user records to maintain a certain real-time.

The main implementation methods are:

Create a new database with field name: ID (character), name (character), user (number) TT (date), admin (permission code, 0-Normal user, 1-admin)

Table name: Online

Header.asp↓

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

<%

... ...

If session ("S_in") <>1 and Session ("s_name") = "then" If user is first logged in

Rs.Open "SELECT * from online", conn,3,3
Rs.addnew
RS ("id") =session.sessionid
RS ("name") = "Visitor" & Session.SessionID
RS ("user") =0 ' 0 means that the user is not logged in, is a tourist identity
RS ("tt") =now
Rs.update
Rs.close
Session ("s_in") =1 ' set user's information has been stored in the database, indicating that it has been online
End If

If session ("S_name") <> "then" if the user has logged in through the login 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's name to the member name
RS ("user") =1 ' indicates that the user has landed and is a member
RS ("tt") =now ' sets the current system time to the user's logon time
Rs.update
Rs.close
End If

... ...

%>

... ...


... ...

<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); 10 seconds to send an update request
</script>

... ...


... ...

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

Onceonline.asp

<%
Rs.Open "Select TT from online where id= '" & Session.SessionID & "'", conn,3,3
RS ("TT") =now () Update online time for current online users
Rs.update
Rs.close

Rs.Open "Delete from online where DateDiff (' s ', Tt,now ()) >60", conn,3,1 ' Delete timeout user
%>

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

In this way, the basic guarantee of the real-time user list in the database, the error depends on the update time and the difference between the time of deletion and the speed of the server processing, we recommend that the time interval for the deletion of the timeout user is too small, which may lead to 0 of people online users error.

The program on the Win2000+sql Server2000 debugging through, because this program on the system requirements are relatively large, expect other friends to come up with a better solution, together to solve this problem!

My mailbox: my5243@hotmail.com

My name is "the building block", welcome you and I become the friend!



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.