返回列表 回复 发帖

如何轻松打造ASP计数器

  1. 以下介绍用数据库实现简单计数器
  2. '下面存为count.asp
  3. <%
  4. Set conn=Server.CreateObject("ADODB.Connection")
  5. conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq="& Server.MapPath("count.mdb")
  6. %>
  7. <%on error resume next%>
  8. <%sql="update count set hit=hit+1%><%conn.Execute(sql)%>
  9. <%sql = "select * from count
  10. set rs=conn.execute(sql)
  11. %>
  12. <%
  13. '更新每周每日数据
  14. lasthit=rs("lasthit")
  15. tdate=year(Now()) & "-" & month(Now()) & "-" & day(Now())
  16. if trim(lasthit)=trim(tdate) then
  17. sql="update site set dayhit=dayhit+1 where id="&request("id")
  18. conn.Execute(sql)
  19. ' response.write "success"
  20. else
  21. sql="update site set dayhit=1 where id="&request("id")
  22. conn.Execute(sql)
  23. ' response.write "error"
  24. end if
  25. sql="update site set lasthit='"&tdate&"' where ID="&request("id")
  26. conn.Execute(sql)



  27. p_year=CInt(year(Now()))-CInt(year(lasthit))
  28. p_month=CInt(month(Now()))-CInt(month(lasthit))
  29. p_day=CInt(day(Now()))-CInt(day(lasthit))
  30. period_time=((p_year*12+p_month)*30+p_day)
  31. if cint(period_time)=<cint(7) then
  32. sql="update site set weekhit=weekhit+1 where id="&request("id")
  33. conn.Execute(sql)
  34. else
  35. sql="update site set weekhit=1 where id="&request("id")
  36. conn.Execute(sql)
  37. end if
  38. %>
  39. document.write('<tr><td width="100%">今日访问<%=rs("dayhit")%>次,本周访问<%=rs("weekhit")%>次,总访问<%=rs("hit")%>次</td></tr>');
  40. <%rs.close
  41. set rs=nothing%>


  42. '用<script language="JavaScript1.1" src="count.asp"></script>在要统计的页面调用即可.

  43. 建立数据库:建一个count的MDB库,再建一个表count,表中字段为
  44. hit 数字型
  45. dayhit 数字型
  46. weekhit 数字型
  47. lasthit 日期型
复制代码
返回列表 回复 发帖