“在线访客”的制作方法

作者:旭旭(07idea)
时间:2003-01-30
E-Mail:[email protected]

======制作原理======

方法就是当用户访问网页时将用户的信息添加进数据库里
在添加的同时,检查数据库里是否有该用户的在线记录,如
果有,则更新该记录,如果没有就把他添加进数据库.
并删除在指定时间内没有活动的在线记录.(大概就是这样吧!)

======数据表设计=======

新建一个数据表,名为"Online"
删除自动编号字段
建立以下字段
字段名:ID 类型:数字
字段名:GUESTNAME 类型:文本
字段名:STATS 类型:文本
字段名:VISITIME 类型:日期/时间
字段名:OUTIME 类型:日期/时间

=======================以下部分源码,供参考,如果写得不好,欢迎指正=======================

 1   
 2sub activeonline() 
 3
 4dim ip 
 5
 6'////删除180秒内不活动的在线记录.   
 7sql="Delete FROM online WHERE DATEDIFF('s',outime,now())>180"   
 8Conn.Execute sql 
 9
10if stats="" then'//如果stats的值为空,则显示为   
11stats="不知在做什么?"   
12else   
13stats=stats   
14end if 
15
16IP=replace(Request.ServerVariables("REMOTE_HOST"),".","")'////获取IP并消去IP中的"." 
17
18'////检查Online表中是否已有这个IP的记录 
19
20sql="select id from online where id='"&ip&"'"   
21set rs=conn.execute(sql) 
22
23if rs.eof or rs.bof then'////如果没有该IP记录则添加在线记录 
24
25sql="insert into online(id,guestname,stats,visitime,outime) values ("&ip&",'游客','"&stats&"',Now(),Now())" 
26
27else'////如果Online表中已有该IP记录则更新该记录 
28
29sql="update online set outime=Now(),stats='"&stats&"',guestname='游客' where id='"&ip&"'" 
30
31end if   
32conn.execute(sql) 
33
34end sub   

==========================实例===========================
将以上代码修改并保存为"Online.asp"嵌入在各网页的尾部

1 
2
3dim conn   
4dim connstr   
5on error resume next   
6connstr="DBQ="+server.mappath("数据库名称.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"   
7set conn=server.createobject("ADODB.CONNECTION")   
8conn.open connstr   
9'保存为conn.asp文件   
 1 
 2
 3dim stats 
 4
 5stats="查看在线" 
 6
 7call activeonline() 
 8
 9  
10Set rs = Server.CreateObject("ADODB.Recordset")   
11sql="SELECT Id,GuestName,Stats,Visitime,Outime FROM Online ORDER BY Visitime Desc"   
12rs.open sql,conn,1,3 
13
14total=rs.RecordCount 
1<table border="1" bordercolor="#111111" cellpadding="2" cellspacing="0" height="53" style="border-collapse: collapse" width="100%">
2<tr>
3<td align="center" height="16" width="20%">昵称</td>
4<td align="center" height="16" width="20%">动作</td>
5<td align="center" height="16" width="20%">来访</td>
6<td align="center" height="16" width="20%">最后活动</td>
7</tr>   

do while not rs.eof

 1<tr>
 2<td align="center" height="28" width="20%">```
 3=rs(1)
 4```</td>
 5<td align="center" height="28" width="20%">```
 6=rs(2)
 7```</td>
 8<td align="center" height="28" width="20%">```
 9=rs(3)
10```</td>
11<td align="center" height="28" width="20%">```
12=rs(4)
13```</td>
14</tr>   

rs.movenext
loop

1</table>

在线人数:``` =total

rs.close
set rs=nothing

1<!--#INCLUDE FILE="Online.asp" -->
2写得不好,见笑了.如果你有更好的方法就献上来吧,大家互相学习嘛!
Published At
Categories with Web编程
Tagged with
comments powered by Disqus