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

2016-01-29 17:19 46 1 收藏

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

【 tulaoshi.com - ASP 】

  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
<TITLECounter 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
<TITLECounter Example 2</TITLE
</HEAD
<BODY
<H1You 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

来源:http://www.tulaoshi.com/n/20160129/1500985.html

延伸阅读
标签: Web开发
代码如下: script Language="javascript"  var seconds = 10;//记数时间  var handle;//事件柄  //开始记数器  function startTimer() {   handle = setInterval("timer()",1000);  }  //结束记数器  function stopTimer() { ...
标签: PHP
  概述: 此设计可以在本计数器基础之上设计计数分析程序,可以对页面访问、ip访问次数进行分析,并形成报表。 一、数据库设计 数据库采用mysql 相关文件:    createDatabase.sql        创建数据库           &nb...
标签: Java JAVA基础
  <!-- JSP-Hitcounter counts sessions. Copyright (C) 2000 Jesper Schmitz Mouridsen. Visit www.webappcabaret/jsm2/webapps.jsp?find=jsphcs for more info. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software...
标签: ASP
  网页计数器DIY 随着网络大行其道,网页计数器也流行起来。事实上大多数网站均有网页计数器,用以反映该网站的访问量。计数器的来源很广,Frontpage等网页编辑器自带了网页计数器,有的站点也提供免费的计数器下载。其实熟悉了ASP编程后,自己做一个计数器很容易。下面介绍一种实现方法。 计数器原理是:在第一次使用网页时置初始值1,以...
标签: PHP
  1)文本计数器 <?php $countfile="/count.txt";  //设置保存数据的文件 if (!file_exists($countfile)){//判断文件是否存在 exec( "echo 0 $countfile"); } $fp = fopen($countfile,"rw"); $length=filesize($countfile); $num = fgets($fp,$length); $num += 1; exec( "rm -rf $...

经验教程

202

收藏

48
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部