您的位置:首页网页设计ASP实例 → 用多种方法制作WEB页面的计数器

用多种方法制作WEB页面的计数器

时间:2004/11/7 2:52:00来源:本站整理作者:蓝点我要评论(1)

One way to do it:



Do you like to know how many users visited your site? Creating a Web counter is very easy thing to do

using ASP. The only thing you have to do is to use an application server variable called count, this

variable will have the value zero when your application is started for the first time, and every time anew

visitor will come to your site the count variable will be incremented, the increment section will be

written in the Session_OnStart in the Global.asa through three steps:

¨  Lock the application server so no more than one visitor will try to increase it at the same time.

¨  Increment the count variable by one.

¨  Unlock the variable.

And that’s all what you have to do to create a simple counter and here is the code.

First let us do the Global.asa section:









And now to show the result you just need to retrieve the count variable like this in your web page:

<%@ language="vbscript"%>





Counter Example 1





You are the visitor number <%=Application("count")%>













Another way to do it:



Ok that’s very nice, but what will happen if the application stops for any reason? You will lose all the

data stored in the application variables and that includes the count variable. To solve this problem I

will use another way, you need to store the value of the count variable with in a text file or database.

For me I prefer database so I will use an MS access database with a table of one field called count and

the field called counter of type NUMBER.



<%

' Declare the variables that will be used with our code

Dim Connection, RS, ConStr, Counts



' Create and open the database connection

Set Connection=Server.Createobjec t("ADODB.Connection")

ConStr=("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath ("counter.mdb"))

Connection.Open ConStr



' Create the record set and retrive the counter value

Set RS = Connection.Execute("SELECT * FROM count")



' Increase the counter value by one

Counts=RS("counter")+1



' Update the counter value in the database

Set RS = Connection.Execute ("UPDATE count SET counter =" & Counts)

Connection.Close

%>







Counter Example 2





You are the visitor number <%=Counts%>













What about Active users:



Some guys wants to know how many visitors are currently seeing the site, for that we will use another way,

we will create an application variable called active, and on each new session we will increase it by one

and when a session ends we will decrease it by one, and here is the code:



The Global.asa:







And to show the results just call the Application variable active in your web page just like this:

<%= Application("Active")%>









I hope you can create your own counters from now on. Give it try and good luck.


相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 迅雷新手完全入门手册 asp下面javascript上传图片限制格式大小方法告诉大家网页弹出窗口应用总结ASP常见错误类型大全asp常见错误分析和解决办法

人气排行 总是弹出visual studio 实时调试器 三种解决SQLSERVER存储过程及调用详解Asp获取真实IP地址ASP中连接Mssql的几种方法一个简单好用的UBB编辑器(含代码)如何用Split将字符串转换为数组并获取数组下ASP防止表单重复提交的办法告诉你免费的简单聊天室源代码