| 首页 | IT动态 | 硬件 | 应用 | 互联网 | 网络 | 网站 | 黑客 | 编程 | 数码 | 视频教程 | 游戏 | 休闲 | 软件 | 视频 | IT认证 | 动漫图片 | IT杂谈 | 论坛 | 
Google
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
没有相关文章
广 告 位 置
黄金广告位
Q
您现在的位置: 无忧IT人 >> 网站 >> ASP >> 文章正文
用ASP建立WEB页面计数器篇         ★★★
用ASP建立WEB页面计数器篇
作者:佚名 文章来源:本站原创 点击数: 更新时间:2006-6-10
[ 字体:缩小 正常 放大 | 双击自动滚屏 ]
请选择合适的字体颜色:
用ASP建立WEB页面的计数器通常有两种简单的方法,一个是建立global.asa,另外一个是直接写一个ASP文件来进行计数。一般使用一个文档来保存浏览数量。

    1、用global.asa来写计数器写一个global.asa文件,存放到虚拟目录的根目录下面,源代码如下:


   <Script language ="Vbscript" Runat="server">
  sub Application_onStart()
    countFile=server.mappath("counter")+"/counter.txt"
    set fso=Server.CreateObject("Scripting.FileSystemObject")
    set file=fso.OpenTextFile("countFile")
      \'读取访问数并赋给Application变量number
    Application("number")=file.readLine
    file.close
  end sub
    sub session_onStart()
      If IsEmpty(Session("hasbeenConnected")) then
        Application.Lock
      Application("number")=Application("number")+1
      Applicaiotn.Unlock
      end if
      Session("hasbeenConnected")=True
    end sub
  sub Application_onEnd()
    countFile=server.mappath("counter")+"/counter.txt"
    set fso=Server.CreateObject("Scripting.FileSystemObject")
    set file=fso.CreateTextFile("countFile",true)
    \'使用writeLine方法写入当前值
    file.writeLine(Application("number"))
    file.close
  end sub
  </script>
    调用计数器时候在网页中写入<%response.Write("你是第"&number&"位访问者!")%>即可,不过调用网页也必须是ASP页面。这种方法有缺点,就是好多个人主页空间并不支持运行global.asa,即不是为用户建的虚拟目录,所以无法正确运行。

    2、直接写一个counter.asp来计数我自己现在用的计数器就是这样,而且可以在任何页面中调用,调用方法是:

  <scriptsrc="http://xxx.xxx.xxx/counter.asp?id=abc&num=6&style=1"></script >
   其中id=abc表示用户名为abc,那么要建立counter/abc.txt存放计数值;
       num=6为计数器的显示位数;
       style=1为计数器风格,建立counter/style1/0~9.gif即可,可以增加多种风格。

   源代码如下:

<%
set fso=server.CreateObject("Scripting.FileSystemObject")
filepath=server.mappath("counter")+"/"+request("id")+".txt"
set temp=fso.opentextfile(filepath,1)
count=temp.readline
temp.close
if isempty(session("connected")) then
  set temp=fso.opentextfile(filepath,2)
  application.lock
  count=count+1
  temp.writeline(count)
  application.unlock
  temp.close
end if
set temp=nothing
set fso=nothing
session("connected")=true
numlength=len(count)
if request("num")="" then
  maxnum=6
else
  maxnum=cint(request("num"))
end if
if request("style")="" then
  style="1"
else
  style=request("style")
end if
for i=1 TO maxnum STEP 1
  If i<=maxnum-numlength  then
    countimage="<img src=http://xxx.xxx.xxx/counter/style"&style&"/0.gif width=15 height=20></img>"
    response.write "document.write(\'"&countimage&"\');"
  Else
    countimage="<img src=http://xxx.xxx.xxx/counter/style"&style&"/"& mid(count,i-(maxnum-numlength),1)&".gif width=15 height=20></img>"
    response.write "document.write(\'"&countimage&"\');"
  End If
next
%>

文章录入:ghost    责任编辑:ghost 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 关于我们 | 友情链接 | 版权申明 |