65.9K
CodeProject 正在变化。 阅读更多。
Home

在您的网站上跟踪用户登录/登出时间

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (26投票s)

2001 年 8 月 19 日

4分钟阅读

viewsIcon

491846

downloadIcon

8959

一个简单的脚本,用于跟踪您网站上用户的登录/登出时间,并显示如何查看在线用户数量。

Sample Image - User_LogTime1.gif

引言

这是一个跟踪网站用户登录/登出时间的脚本。这是一个我用在我开发的一些网站上的简单脚本。此外,使用此脚本,您可以查看您网站上有多少用户在线。

为此,您首先需要一个具有“登录进入”(会员制社区网站)的网站。您还需要一个数据库来保存用户及其登录/登出时间的记录。您还需要 global.asa 文件,以便您可以使用 Session_OnEnd 事件来跟踪 Session.Abandon 发生或 Session.Timeout 过期的时间。那时用户会点击注销或退出您的应用程序。

数据库

此演示使用的数据库是 MS Access 2000 数据库(.mdb 文件),它包含 2 个表:Members 表和 User_LogTimes 表。在 Members 表中,我保存了最少的登录信息,例如:用户名、密码、用户名字和用户姓氏。

在下面的图片中,您可以看到 Members 表的设计。此表与本文无关,但我之所以提及它,是为了让您能够理解文章后面将要介绍的 SQL 查询。

Members Table design

现在,看看 User_LogTime 表的设计。在此表中,我们将保存用户的登录/登出时间记录,并且我们可以从中获取您网站上有多少用户在线。

在下面的图片中,您可以看到表的设计。

User_LogTime Table Design

这是表字段的简要描述。

id - 它是此表的主键,用于唯一标识每条记录
user_id - 来自 Members 表的用户 ID,将在下面的图片中显示
SID - 当用户第一次运行您应用程序中的任何页面时,网站上每个已打开会话的 Session.SessionID
Login_Time - 用户登录网站的时间。
Logout_Time - 用户从网站注销的时间。
offline - 布尔值,用于指示网站上有多少用户在线。

此脚本如何工作

login.asp

当用户登录网站时,login.asp 中的脚本将首先执行。这就是为什么我将首先解释此代码。

使用登录表单中输入的用户名和密码,您将构建一个查询,从 Members 表中获取用户的 id 值。该值将存储在会话变量中。

' Get the user name and the password from the login form
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 the user id from the database
set rs = conn.Execute (query)
...
	
...		
' set the user id value from the Members table in a session variable
Session("member") = rs("Id")

您在 Session("member") 变量中有用户 ID 值。现在,我们需要修改 User_LogTime 表中该用户的、offline 字段设置为 False(这意味着他被标记为在线)的所有记录,并将其设置为 True。这样更改的字段将没有 LogOut 时间。通常,此操作不应影响表中的任何记录。offline 设置为 False 的记录可能存在于用户之前注销时未正确关闭的情况。

然后,我们将向 User_LogTime 表插入一条新记录,其中包含用户 ID、该用户的 SessionID 和登录时间。

' Modify all the records from the User_LogTime corresponding to this user.
query = "UPDATE User_LogTime SET offline=True WHERE offline=False AND user_id=" &_
        session("member")
conn.Execute (query)
				
' Insert a new record in the User_LogTime table with the user_id, 
' SessionID and the login time.
query = "INSERT INTO User_LogTime (user_id, SID, Login_Time) "
query = query &_
        "VALUES (" & Session("member") & "," &_
        Session.SessionID & ",#" & now() & "#)"
conn.Execute (query)

global.asa

上面的代码在会话开始时被调用,您获取了登录时间并将其写入数据库。

当用户点击注销时,将调用 Session.Abandon。如果用户在未点击注销的情况下退出您的应用程序,那么在 session.Timeout 过期后,将执行 Session_OnEnd。在此过程中,您将更新 login.asp 中写入的记录,并将 Logout_timeoffline 字段更新为 True

Sub Session_OnEnd
	set conn = Server.CreateObject ("ADODB.Connection")
	conn.Open Application("connString")

	' Update the record when the user logout and write the logout time 
	' plus it sets the user as 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

这就是跟踪登录/登出时间的所有内容。您可以在 User_LogTimes 中找到这些值,并且可以根据需要显示它们。您可以查看我在项目演示中是如何实现的(参见上面的第 1 张图片)。

在线用户

数据库中存在 offline 字段,这使得此操作非常简单。您可以执行类似这样的 SQL 查询

query = "SELECT DISTINCT user_id FROM User_LogTime WHERE offline=False"

执行该查询,记录集中的记录数就是您在线用户的数量。或者您可以调用

query = "SELECT COUNT(*) as NumOnline FROM User_LogTime WHERE offline=False"

返回一个包含一条记录和一个字段(“NumOnline”)的记录集,其中包含在线用户数量。

有关示例,请参阅演示应用程序。

备注

  • 当您将 SessionID 存储在数据库中时,该字段的数据类型始终设置为数字。如果您想将该字段的值与 Session.SessionID 进行比较,这将为您省去一些麻烦。
  • 在 PWS on Win95/98 上,这应该可以正常工作,但在 IIS5 上,请记住,此 Session_OnEnd 将由 IWAM_machine 而不是 IUSR 运行,因此请为将保存登录/登出时间的数据库设置写入/修改权限。

编程愉快!!

© . All rights reserved.