asp最简单最实用的计数器

所属分类: 网络编程 / ASP编程 阅读数: 611
收藏 0 赞 0 分享
刚才找一个计数器,由于网站的访问量太少,放个计数器在那里确实有点寒酸了,于是呼只能搞一个简单点的,可以访问一次就记录一次的来撑撑门面先。本来以前我有一个,但是郁闷的是居然找不到了,在网上SO了一圈,总算是搞定了,具体程序代码如下。
  count.asp的代码为: 

复制代码 代码如下:

<% 
    option explicit 
  dim fs,filename,txt,content,total,counter_lenth 
  counter_lenth=1  '设置显示数据的最小长度,如果小于实际长度则以实际长度为准 
  set fs=Server.CreateObject("Scripting.FileSystemObject") 
  filename=server.MapPath("count.txt") 
  if not fs.FileExists(filename) then 
    fs.CreateTextFile filename,True,True 
    set txt=fs.OpenTextFile(filename,2,true) 
    txt.write 0 '如不存在保存数据的文件则创建新文件并写入数据0 
    set fs=nothing 
  end if 

  set txt=fs.OpenTextFile(filename) 
  If txt.AtEndOfStream Then 
    Application("Counter")=0 '如果文件中没有数据,则初始化Application("Counter")的值(为了容错) 
  else 
    Application("Counter")=txt.readline 
  end if 

  Application.Lock  
  Application("Counter") = Application("Counter") + 1 
  Application.UnLock 


  Function save_ '保存计数函数 
  set fs=Server.CreateObject("Scripting.FileSystemObject") 
  filename=server.MapPath("count.txt") 
  content=Application("Counter") 
  set txt=fs.OpenTextFile(filename,2,true) 
  txt.write content 
  set fs=nothing 
  End Function 

  save_  '调用保存函数保存数据 

  Function Digital ( counter )  '显示数据函数 
    Dim i,MyStr,sCounter 
     sCounter = CStr(counter) 
    For i = 1 To counter_lenth - Len(sCounter) 
      MyStr = MyStr & "0" 
    'MyStr = MyStr & "<IMG SRC=改成你自己的图片存放的相对目录\0.gif>" '如有图片,可用此语句调用 
    Next 
    For i = 1 To Len(sCounter) 
      MyStr = MyStr & Mid(sCounter, i, 1) 
    'MyStr = MyStr & "<IMG SRC=改成你自己的图片存放的相对目录\" & Mid(sCounter, i, 1) & ".gif>" '如有图片,可用此语句调用 
    Next 
    Digital = MyStr 
  End Function 

  Function count_show  '读取计数函数 
  set fs=Server.CreateObject("Scripting.FileSystemObject") 
  filename=server.MapPath("count.txt") 
    set txt=fs.opentextfile(filename,1,true) 
  total=txt.readline 
  total=cint(total) 
  'response.write total 
  response.write Digital (total) '调用显示函数 
  set fs=nothing 
  End Function 

%>



  然后新建一个count.txt(必须和count.asp同一级目录),打开这个文件后在里面输入任意数字(别太狠啦),然后在需要显示计数器的那个页面顶部加入
复制代码 代码如下:

<!--#include file="count.asp"--> 


最后在需要显示计数器的地方加上代码
复制代码 代码如下:

<%=count_show%> 


就OK了。
更多精彩内容其他人还在看

IIS访问ASP页面时报错The requested resource is in use.的解决办法

IIS访问ASP页面时报错The requested resource is in use.的解决办法
收藏 0 赞 0 分享

错误类型:Provider (0x80004005)未指定的错误 的一个处理方法

一般情况下asp可以正常运行,但只要连接数据库就提示,Microsoft JET Database Engine 错误'80004005'
收藏 0 赞 0 分享

关于“未指定的错误”的问题 的比较正解的解决方法

关于“未指定的错误”的问题 的比较正解的解决方法
收藏 0 赞 0 分享

关于asp+access的安全问题分析

关于asp+access的安全问题分析
收藏 0 赞 0 分享

把RS.GetRows看得更清楚

把RS.GetRows看得更清楚
收藏 0 赞 0 分享

ASP面向对象编程探讨及比较

ASP面向对象编程探讨及比较
收藏 0 赞 0 分享

ASP错误处理

ASP错误处理
收藏 0 赞 0 分享

web文件管理器的后续开发

web文件管理器的后续开发
收藏 0 赞 0 分享

上一篇,下一篇过程代码

上一篇,下一篇过程代码
收藏 0 赞 0 分享

一小偷类!!有兴趣的可以看看

一小偷类!!有兴趣的可以看看
收藏 0 赞 0 分享
查看更多