网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP实例
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.网络寻呼机数据库版总结.
.天气预报的小偷,可以偷到全国24小.
.几例在ASP存储过程的使用方法.
.利用ASP存取各种常用类型数据库(.
.使用AspImage制作图形(二).
.Web在线代理(Asp版).
.asp实现k线图(在线).
.ASP实现多域名同一空间的处理实例.
.用ASP技术编制隐藏用户密码程序.
.用ASP和SQL实现基于Web的事件日历.
.完整的访问统计程序(三 应用篇).
.纯ASP(VBscript)写的全球IP地址搜.
.用ASP开发一个在线考试程序(九).
.制作一个简单的服务器端控制.
.用ASP.NET构建完整E-mail发送系统.
.一个投票系统的源程序(coveryour.
.DW+ASP 玩转动态二级菜单.
.一个BBS的源代码(四).
.聊天室建设详解四.
.巧用ASP生成PDF文件.

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

发表日期:2000-12-4


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:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart
   ' Lock the Application object
   Application.Lock

     ' Increment the count
      Application("count") = Application("count") + 1

   ' Unlock the Application object
   Application.Unlock
End Sub
</SCRIPT>


And now to show the result you just need to retrieve the count variable like this in your web page:
<%@ language="vbscript"%>
<HTML>
<HEAD>
<TITLE>Counter Example 1</TITLE>
</HEAD>
<BODY>
<H1> You are the visitor number <%=Application("count")%></H1>
</BODY>
</HTML>



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
%>

<HTML>
<HEAD>
<TITLE>Counter Example 2</TITLE>
</HEAD>
<BODY>
<H1>You are the visitor number <%=Counts%></H1>
</BODY>
</HTML>



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:
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

Sub Application_OnStart
         'Create the variable that will hold the number of active visitors
    Application("Active") = 0
End Sub

Sub Session_OnStart
   'Increase the counter by one
   Application.Lock
      Application("Active") = Application("Active") + 1
   Application.UnLock

End Sub

Sub Session_OnEnd
   ' Decrease the c ounter
   Application.Lock
      Application("Active") = Application("Active") - 1
   Application.UnLock
End Sub

</SCRIPT>


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.
上一篇:用ASP+实现一个简单的计算器(适合入门者) 人气:11963
下一篇:asp+的论坛列表程序---代码部分 人气:10060
浏览全部的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐