Research on asp forum online user count statistics

Source: Internet
Author: User

1. Create a database table
Table Name: online
Set the following fields
Id' is used to record the session. sessionid of each access.
Name' if it is a visitor, it is recorded as a visitor.
Online, if the access is 0, if the access is 1
Datetime 'latest activity time
Username' indicates the username of the member. The guest is empty.
Ip address 'record logon IP addresses for all accesses

Head. asp: the number of online users written to the database table. This page must be searched into every ASP page used to browse.

<%
Set rs = Server. CreateObject ("ADODB. Recordset ")
If session ("username") = "" then determines that the user is not logged on
SQL = "select * from online where id = '" & session. sessionid & "'" 'determines whether the sessionid exists in the Database Table.
Rs. Open SQL, Conn, 1, 3
If rs. eof then' the visitor browsed for the first time
Rs. addnew
Rs ("id") = session. sessionID
Rs ("name") = "Tourists"
Rs ("online") = 0' 0 indicates that the user has not logged in. It is a tourist identity.
Rs ("datetime") = now ()
Userip = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If userip = "" Then
Userip = Request. ServerVariables ("REMOTE_ADDR ")
End if
Rs ("ip") = userip
Else 'guest not the first visit

Rs ("datetime") = now () 'update activity time
Rs. update
Rs. close
End if
Else
SQL = "select * from online where id = '" & session. sessionID & "'or admin ='" & session ("username") & "'"' determines that the sessionid or user name record already exists in the data table.
Rs. Open SQL, Conn, 1, 3
If rs. eof then
Rs. addnew' members log on to the website for the first time (they may log on to the forum directly from the homepage ).
Rs ("id") = session. sessionID
Rs ("name") = session ("show") 'write user nickname
Rs ("username") = session ("username") 'write the Login User Name
Rs ("online") = 1' indicates that the user has logged on and is a member.
Rs ("datetime") = now () 'sets the current system time to the user's logon time
Userip = Request. ServerVariables ("HTTP_X_FORWARDED_FOR ")
If userip = "" Then
Userip = Request. ServerVariables ("REMOTE_ADDR ")
End if
Rs ("ip") = userip
Else // the member does not browse the website for the first time and the visitor logs on to the website.
Rs ("name") = session ("show") update user nickname
Rs ("username") = session ("username ")
Rs ("online") = 1' indicates that the user has logged on and is a member.
Rs ("datetime") = now ()
End if
Rs. update
Rs. close
End if
Set rs = nothing
%>
Conn.exe cute ("delete from online where datediff ('s ', datetime, now ()> 60")' deletes a visitor who has no activity for 60 seconds. You can adjust the time by yourself.

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.