手机站
网通分站
电信主站
密 码:
用户名:
热门关键字:  虚拟主机  cn域名  域名注册  非法  seo
当前位置 : 主页>网络编程>Asp编程>列表

ASP建立WEB页面计数器

来源:互联网 作者:west263.com 时间:2008-02-23 点击:
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
  用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=/info/upimg/allimg/080226/0855450.jpg 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
%>

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名
注册