用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
%>
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器