日期:2011-02-06  浏览次数:20806 次

“技术天地”中的《编写ASP图形计数器》一文,详细的说明了如何利用流行的ASP来编写计数器。但是,美中不足的是,如果某个用户反复点击“刷新”按钮,那么计数器还是要不断的增加的,这对网站点击率评比来说是不公平的,也失去了计数器做为正常统计功能的作用。如何在技术上避免这种情况的发生呢?
我认为要防止上网用户连续按下“刷新”计数器也连续增加的问题,最好的办法就是利用ASP的Session对象,我们可以借助Session对象首先判断该用户是否为新连接者,如果是,那么

IsEmpty(Session("hasbeenConnected"))=True,也就是说,Session("hasbeenConnected")是空的。那么,运行程序,使计数器加1,然后将该用户的Session("hasbeenConnected")设置为True,也就是说,这个用户已经不是新的连接者,无论他怎么连续按下“刷新”按钮,计数器也不会再增加。

结合《编写ASP图形计数器》一文,最后的站点计数器的源程序应该是:

<html>
<head>
……
</head>
<body>
<%dim tms,counter,countlen
dim images(20)
h1="<p><font color='#8000ff'>这是一个ASP计数器</font></p>"
If IsEmpty(Session("hasbeenConnected")) then
set rs=server.createobject("adodb.recordset")
application.lock
rs.open "update aspcount set countss=countss+1","dsn=userdbs",3,3
application.unlock
Session("hasbeenConnected")=True
End If
set rs=server.createobject("adodb.recordset")
rs.open "select * from aspcount","dsn=userdbs",3,3
rs.movefirst
counter=rs(0)
countlen=len(counter)
tms="<h1><font color='#8000ff'>您是第</font>"&&counter&&"<font color='#8000ff'>位访问者!</font></h1>"
response.write(tms)
for i=1 to countlen
images(i)="<img src=" && mid(counter,i,1) && ".gif></img>"
next
response.write images(1)&&images(2)&&images(3)&&images(4)&&images(5)&&images(6)&&images(7)
rs.Close
%>
</body>
</html>