|          
大家好!今天我想谈谈一个网页的计数器问题,有些网站的计数器在用户刷新一次页面时,记数值会加1,这样的计数器是不健全的。如果你的网站搞一些有奖活动,例如,"如果你是第100000个用户你将得到本站提供的奖品"。
 所以,下面给出一个计数器的原码,同时你要在这个ASP文件下新建一个counter.txt文件,用于存放记数值。
 <%
 function counts(counterfile)
 
 dim objfso,objts
 application.lock    '锁定对象
 set objfso=server.createobject("scripting.filesystemobject")
 set objts=objfso.opentextfile(server.mappath(counterfile),1,true)
 if not objts.atendofstream then    '检查是否到达文件结尾
 counts=clng(objts.readline)
 end if
 counts=counts+1
 objts.close
 set objts=objfso.opentextfile(server.mappath(counterfile),2,true)
 objts.writeline(counts)
 objts.close
 application.unlock    '解除锁定
 end function
 
 if session("counter")=empty then
 session("counter")=counts("counter.txt")
 end if
 %>
 
 <html>
 <body>
 您是第<%=session("counter")%>位访客
 </body>
 </html>
 这样一个完美的计数器就做成了。
 |