ASP建立WEB页面计数器

王朝asp·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

用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

%>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航