用户登录
用户注册

分享至

asp最简单最实用的计数器

  • 作者: 狠不下心28213228
  • 来源: 51数据库
  • 2021-10-06
刚才找一个计数器,由于网站的访问量太少,放个计数器在那里确实有点寒酸了,于是呼只能搞一个简单点的,可以访问一次就记录一次的来撑撑门面先。本来以前我有一个,但是郁闷的是居然找不到了,在网上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了。
软件
前端设计
程序设计
Java相关