原创文章,摘录请指明出处作者:Juven 摘自:It365cn.com
原主题贴:
http://www.it365cn.com/bbs/edit.asp?topicid=642 参看效果:http://www.it365cn.com/bbs/online.asp 制作思路: 方法就是当用户访问网页时将用户的信息添加进指定数据表Online,在添加的同时,检查数据表Online是否有该用户的在线记录,如果存在,则更新该用户登录记录信息,如果没有就把他添加进数据表Online中,并删除在指定时间内没有活动的在线记录。
数据表Online设计: o_username 在线用户名 字符型
o_ip 在线用户IP值 字符型
o_lanmu 所在BBS栏目 数字型
o_topic 所在主题 数字型
o_lasttime 登录时间 日期型
o_system 用户系统信息 字符型
登录BBS,记载用户信息(bbsonline.asp):<%
'浏览器、操作系统版本侦测
function browser(text)
if Instr(text,"MSIE 5.5")>0 then
browser="IE 5.5"
elseif Instr(text,"MSIE 6.0")>0 then
browser="IE 6.0"
elseif Instr(text,"MSIE 5.01")>0 then
browser="IE 5.01"
elseif Instr(text,"MSIE 5.0")>0 then
browser="IE 5.00"
elseif Instr(text,"MSIE 4.0")>0 then
browser="IE 4.01"
else
browser="未知"
end if
end function
function system(text)
if Instr(text,"NT 5.1")>0 then
system=system+"Windows XP"
elseif Instr(text,"NT 5")>0 then
system=system+"Windows 2000"
elseif Instr(text,"NT 4")>0 then
system=system+"Windows NT4"
elseif Instr(text,"4.9")>0 then
system=system+"Windows ME"
elseif Instr(text,"98")>0 then
system=system+"Windows 98"
elseif Instr(text,"95")>0 then
system=system+"Windows 95"
else
system=system+"未知"
end if
end function
'用户浏览环境
o_ip=Request.ServerVariables("REMOTE_ADDR")
if Request.ServerVariables("HTTP_X_FORWARDED_FOR")<>"" then o_ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
o_system=system(Request.ServerVariables("HTTP_USER_AGENT"))&","&browser(Request.ServerVariables("HTTP_USER_AGENT"))
o_username=bbsusername 'bbsusername登录用户名,这在用户系统登录时程序所记录的
if o_username = "" then o_username = "客 人"
sqlonline = "select * from online where o_ip = '" & o_ip & "'"
set rsonline =conn.Execute (sqlonline)
if not rsonline.eof then
sqlonline = "update online set o_hidden="&bbshidden&",o_system='"&o_system&"',o_username = '"&o_username&"', o_LastTime = '" &now()&"' where o_ip = '" & o_ip & "'"
conn.Execute (sqlonline)
else
sqlonline = "insert into online (o_hidden,o_username, o_ip,o_LastTime,o_system,o_topic) values ("&bbshidden&",'" & o_username & "', '" & o_ip & "','"&now()&"','" & o_system & "',0)"
conn.Execute (sqlonline)
end if
if o_username <> "客 人" then
sqlonline = "delete from online where o_username = '" & o_username & "' and o_ip <> '" & o_ip & "'"
conn.Execute (sqlonline)
end if
sqlonline = "select o_lasttime,o_username from online"
set rsonline = conn.Execute (sqlonline)
if not rsonline.eof then
do while not rsonline.Eof
if DateDiff("n",rsonline("o_lasttime"),now())>bbs_seconds then 'n分钟内没动作就离开在线
conn.Execute ("update [user] set lastlogin='"&now()&"' where username = '" &rsonline("o_username")&"'")
conn.Execute ("delete from online where o_username = '" &rsonline("o_username")&"'")
end if
rsonline.movenext
loop
end if
%>
其中:
bbsusername是登录用户名,这在用户系统登录时程序所记录的(用户登录系统,非本文所述,请查看相关文档);
bbs_seconds是指定时间内没有活动的变量值,数字型的。
这样,数据表online就记录了登录用户的相关信息,下面介绍如果在前台显示在线用户。
显示在线用户信息online.asp:<%
set rs = conn.execute ("select * from online order by o_lasttime desc")
'显示在线用户名
if rs("o_username")<>"客 人" then
response.write ""&rs("o_username")&""
else
response.write rs("o_username")
end if
'用户当前所在BBS位置
if rs("o_lanmu")="0" then
response.write("论 坛 首 页")
elseif rs("o_lanmu")="-1" then
response.write "查看论坛在线人员"
elseif rs("o_lanmu")="-2" then
response.write "精华归档区"
elseif rs("o_lanmu")="-3" then
response.write rs("o_username")&"的控制面板区"
elseif rs("o_lanmu")="-4" then
response.write rs("o_username")&"的短消息区"
else
sql1="select Forumtitle,forumid from Forum where forumid="&rs("o_lanmu")&""
set rs1=conn.execute(sql1)
response.write ""&rs1("forumtitle")&""
rs1.close
set rs1=nothing
end if
'rs("o_lanmu")中0、-1、-2等值是登录BBS版块文件list.asp记录下来的变量值。Forum是BBS版块名称的数据表。
'显示用户当前具体位置
if trim(rs("o_topic"))="0" then
response.write("论坛列表区")
elseif rs("o_topic")="-2" then
response.write "精华归档区"
elseif rs("o_topic")="-3" then
response.write rs("o_username")&"的控制面板区"
elseif rs("o_topic")="-4" then
response.write rs("o_username")&"的短消息区"
else
set rs1=conn.execute("select t_title from topic where topicid="&rs("o_topic")&"")
if rs1.eof then
response.write "查看的主题贴已不存在"
else
response.write ""&htmlencode(rs1("t_title"))&""
end if
rs1.close
set rs1=nothing
end if
'rs("o_topic")中0、-1、-2等值是察看BBS主题贴文件topic.asp记录下来的变量值。topic是BBS保存主题贴的数据表。
'显示用户信息系统信息
response.write rs("o_system")
'显示用户IP
o_ip1=rs("o_ip")
oip = Split(o_ip1,".")
if Ubound(oip)<3 then'多这个判断是为了防止有些ip为空的
response.write o_ip1
elseif issupermaster then
response.write(oip(0)&"."&oip(1)&"."&oip(2)&"."&oip(3))
else
response.write oip(0)&"."&oip(1)&"."&oip(2)&".*"
end if
'显示登录时间
response.write rs("o_lasttime")
%>
核心代码制作完成,然后用循环Do while not rs.eof ... rs.movenext ... loop从数据表online中读取所有在线用户记录,这里我们就不多做介绍了。