We don't need to use global. Asa to discuss a good way to count ASP online users.

Source: Internet
Author: User
Tags servervariables

 

There are many ways to count online users in ASP, and I cannot figure out several ^ _ ^ methods. I would like to introduce three methods first. If there are any errors, please criticize and correct them. There are also good methods, please add it!

First, we will introduce a simple method, using application, session, and global. asa, because when the web pages created with ASP are stored in the base point directory of the WWW server and the WWW server is started, when there is an HTTP request to the ASP file, the server will read global under the base point directory. asa file. Therefore, you can include the following code in the global. Asa file:

<%

'--------- Define variables

Dim rstj

Dim IP

Dim timeouted

Dim X

Dim conntj

Dim dbpath

'--------- Define variable end

'--------- Suggested database connection
Set conntj = server. Createobject ("ADODB. Connection ")

Dbpath = server. mappath ("count. mdb ")

Conntj. Open "driver = {Microsoft Access Driver (*. mdb)}; DBQ =" & dbpath

'--------- Database connection established

'--------- Create a database rstj object

Set rstj = server. Createobject ("ADODB. recordset ")

'--------- Database rstj object created

'--------- Read Client IP Address

IP = request. servervariables ("http_x_forwarded_for") 'if the other party uses a proxy server to access the Internet, use request. servervariables ("http_x_forwarded_for") can obtain the real IP address of the other party. If the other party does not access the Internet through the proxy server, the IP address value is blank.

If IP = "" Then IP = request. servervariables ("remote_addr") 'If the IP value is null, obtain its local client address.

'--------- IP address read is complete

'--------- Read the number of new content added to the database in the last 20 minutes. Group by IP-the same table IP value is recorded as 1

SQL = "select IP from Count where posttime> = dateadd ('n',-20, now () group by IP"

Rstj. Open SQL, conntj, 1, 1

Online = rstj. recordcount

Rstj. Close

'--------- Get the online person value

'--------- Check whether the database has the same value. If not, x = "yes" indicates that X = "no"

SQL = "select IP from Count where IP = '" & IP &"'"

Rstj. Open SQL, conntj, 1, 1

If rstj. EOF and rstj. bof then

X = "yes"

Else

X = "no"

End if

Rstj. Close

'-------- Judgment complete

'-------- Add a new value if the database does not have the same value

If X = "yes" then', add a record if this IP address does not exist.

SQL = "select top 1 * from Count"

Rstj. Open SQL, conntj, 1, 3

Rstj. addnew

Rstj ("ip") = IP

Rstj ("posttime") = now ()

Rstj. Update

Rstj. Close

Else' if this IP address exists, change the time to the current time.

SQL = "select * from Count where IP = '" & IP &"'"

Rstj. Open SQL, conntj, 1, 3

Rstj ("posttime") = now ()

Rstj. Update

Rstj. Close

End if

'-------- Check that join is complete

'-------- Delete the value added 20 minutes ago

Timeouted = dateadd ("N",-20, now ())

SQL = "delete * from Count where posttime <#" & timeouted &"#"

Conntj. Execute SQL

'-------- Deleted

'-------- Close the Data Object

Set rstj = nothing

Conntj. Close

Set conntj = nothing

%>

Document. Write ('current person online <% = online %> person ');

The call method is as follows. Insert the following code where you want to display it (note that the path must be correct !) :

 

Reference:

<Script language = "JavaScript" src = "online. asp"> </SCRIPT>

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.