Record the time of stay at the website

zhaozj2021-02-16  56

Record the time of stay at the website

We always pay attention to the traffic of the website, but the traffic of the website does not explain how much attraction is your web page. Can more accurately explain your website attraction and is the time for users stay on your website. The idea in this article is to achieve this feature, and it also knows how many users currently have.

In order to implement this function, first, a website must have "landing box". You need a database (record the user and the time they log in and logout), you need the global.asa file (so you can use the session_onend event tracking time when session.ABandon or session.Timeout is terminated. This time is when the user hits "Logout" or exits the time when your program is exited).

database

The database uses the MS Access 2000 database (MDB file). It contains two tables: MEMBERS table and user_logtimes table. Keep some login information in the Members table such as User Name, Password, User First Name, and User Last Name. In the user_logtime table, the user who has retained the user login / logout is retained, and from this table we can know how many users have online online.

The design of the database is ourselood.

script

Login.asp

When a user logs in on your site, the script in login.asp is first performed. When the user enters the username and password in the login box, you need to create a query to get the user ID from the Members table. This value will be stored in a dialog variable:

'Get username and password from the login box

UserName = Request.form ("Username")

Password = Request.form ("password")

...

...

Set conn = server.createObject ("adoDb.connection")

Conn.open Application ("Connstring")

Query = "Select id from members where username = '" & _ username & "' and password = '" & password & _ ""

'Get user ID from the database

SET RS = conn.execute (query)

...

...

'Set user ID value from the Members table in the dialog variable

Session ("MEMBER") = rs ("id")

After the user ID in the Session ("Me") variable, we must now modify this of the user_logtime table set all the records of the Offline field to false (meaning he has already online), and also put that The field is set to True. This changed field will not have a logout time. General This operation does not affect any records in the table. The record of the "OFFLINE" field is set to false may exist that when the user is not properly turned off or the user is rushing.

Then we use the user ID (user_id) in the user_logtime table, insert a new record into a new record in the user ID (User_ID), the user's sessionID and the login time.

'Corresponding to all records of this user from USER_LOGTIME

Query = "update user_logtime set offline = true where offline = false and user_id =" & _ session ("member") conn.execute (query)

Insert a new record in User_ID, SessionID and Login Time in the user_logtime table

Query = "INSERT INTO USER_LOGTIME (user_id, sid, login_time"

Query = query & _ "VALUES (" "& Session (" MEMBER ") &", "& _ session.SessionID &" # "& now () &" #) "

Conn.execute (query)

Global.asa

When a conversation begins to be called, you get the login time, then write them into the database.

When the user hits "logout", then Session.Abandon is called. If the user does not click "Logout" to exit the program, SESSION_ONEND will be executed when session.timeout is terminated. In this process you update the record that has been written into the login.asp, then update logout_time, modify the offline field for true.

SUB session_onend

Set conn = server.createObject ("adoDb.connection")

Conn.open Application ("Connstring")

'When the user logs out, the record is updated, write down the logout time, and then set the user offline.

Query = "Update User_Logtime Set Logout_time = #" & now () & "#, offline = true"

Query = query & "where offline = false and sid =" & session.SessionID & _ "and user_id =" & session ("member")

Conn.execute (query)

Conn.close

Set conn = Nothing

End Sub

online user

With the OFFLINE field, this problem makes it easy. You can generate a SQL query, like this:

Query = "SELECT DISTINCT User_id from user_logtime where offline = false"

Execute a query, the number in the record will be the number of your online users. You can have a choice:

Query = "SELECT Count (*) AS Numonline from user_logtime where offline = false

Returns a record with a record, the field ("Numonline") is the number of online users.

In this way, we have completed the model of this instance, and the rest is the work of some fine branches. We can extend its features. But pay attention to PWS on Win95 / 98 is very good, but in IIS5, I remember that session_onend runs through IWAM_MACHINE instead of IUSR. So in the database with login / logout time, you must set the right to write / modify it. Record the time of stay at the website

We always pay attention to the traffic of the website, but the traffic of the website does not explain how much attraction is your web page. Can more accurately explain your website attraction and is the time for users stay on your website. The idea in this article is to achieve this feature, and it also knows how many users currently have. In order to implement this function, first, a website must have "landing box". You need a database (record the user and the time they log in and logout), you need the global.asa file (so you can use the session_onend event tracking time when session.ABandon or session.Timeout is terminated. This time is when the user hits "Logout" or exits the time when your program is exited).

database

The database uses the MS Access 2000 database (MDB file). It contains two tables: MEMBERS table and user_logtimes table. Keep some login information in the Members table such as User Name, Password, User First Name, and User Last Name. In the user_logtime table, the user who has retained the user login / logout is retained, and from this table we can know how many users have online online.

The design of the database is ourselood. See the design below for details:

script

Login.asp

When a user logs in on your site, the script in login.asp is first performed. When the user enters the username and password in the login box, you need to create a query to get the user ID from the Members table. This value will be stored in a dialog variable:

'Get username and password from the login box

UserName = Request.form ("Username")

Password = Request.form ("password")

...

...

Set conn = server.createObject ("adoDb.connection")

Conn.open Application ("Connstring")

Query = "Select id from members where username = '" & _ username & "' and password = '" & password & _ ""

'Get user ID from the database

SET RS = conn.execute (query)

...

...

'Set user ID value from the Members table in the dialog variable

Session ("MEMBER") = rs ("id")

After the user ID in the Session ("Me") variable, we must now modify this of the user_logtime table set all the records of the Offline field to false (meaning he has already online), and also put that The field is set to True. This changed field will not have a logout time. General This operation does not affect any records in the table. The record of the "OFFLINE" field is set to false may exist that when the user is not properly turned off or the user is rushing.

Then we use user ID (user_id) in the user_logtime table to insert a new record inserted into a new record in the user's sessionID, and user's login time: 'Corresponding to all records of this user from User_logtime

Query = "Update user_logtime set offline = true where offline = false and user_id =" & _ session ("member")

Conn.execute (query)

Insert a new record in User_ID, SessionID and Login Time in the user_logtime table

Query = "INSERT INTO USER_LOGTIME (user_id, sid, login_time"

Query = query & _ "VALUES (" "& Session (" MEMBER ") &", "& _ session.SessionID &" # "& now () &" #) "

Conn.execute (query)

Global.asa

When a conversation begins to be called, you get the login time, then write them into the database.

When the user hits "logout", then Session.Abandon is called. If the user does not click "Logout" to exit the program, SESSION_ONEND will be executed when session.timeout is terminated. In this process you update the record that has been written into the login.asp, then update logout_time, modify the offline field for true.

SUB session_onend

Set conn = server.createObject ("adoDb.connection")

Conn.open Application ("Connstring")

'When the user logs out, the record is updated, write down the logout time, and then set the user offline.

Query = "Update User_Logtime Set Logout_time = #" & now () & "#, offline = true"

Query = query & "where offline = false and sid =" & session.SessionID & _ "and user_id =" & session ("member")

Conn.execute (query)

Conn.close

Set conn = Nothing

End Sub

online user

With the OFFLINE field, this problem makes it easy. You can generate a SQL query, like this:

Query = "SELECT DISTINCT User_id from user_logtime where offline = false"

Execute a query, the number in the record will be the number of your online users. You can have a choice:

Query = "SELECT Count (*) AS Numonline from user_logtime where offline = false

Returns a record with a record, the field ("Numonline") is the number of online users. In this way, we have completed the model of this instance, and the rest is the work of some fine branches. We can extend its features. But pay attention to PWS on Win95 / 98 is very good, but in IIS5, I remember that session_onend runs through IWAM_MACHINE instead of IUSR. So in the database with login / logout time, you must set the right to write / modify it.

转载请注明原文地址:https://www.9cbs.com/read-27778.html

New Post(0)