频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> asp教程 -> 用多种方法制作WEB页面的计数器

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

作者:未知  来源:转载  发布时间:2005-7-20 11:38:31  发布人:acx

减小字体 增大字体

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:

<&#115cript LANGUAGE=VB&#115cript 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
</&#115cript>


And now to show the result you just need to retrieve the count variable like this in your web page:
<%@ language="vb&#115cript"%&gt;<br>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;TITLE&gt;Counter Example 1&lt;/TITLE&gt;<br>&lt;/HEAD&gt;<br>&lt;BODY&gt;<br>&lt;H1&gt; You are the visitor number &lt;%=Application("count")%&gt;&lt;/H1&gt;<br>&lt;/BODY&gt;<br>&lt;/HTML&gt;<br><br><br><br>Another way to do it:<br><br>Ok that’s very nice, but what will happen if the application stops for any reason? You will lose all the <br>data stored in the application variables and that includes the count variable. To solve this problem I <br>will use another way, you need to store the value of the count variable with in a text file or database. <br>For me I prefer database so I will use an MS access database with a table of one field called count and <br>the field called counter of type NUMBER.<br><br>&lt;%<br>' Declare the variables that will be used with our code<br>Dim Connection, RS, ConStr, Counts<br><br>' Create and open the database connection<br>Set Connection=Server.Createobjec t("ADODB.Connection")<br>ConStr=("DRIVER=&#123;Microsoft Access Driver (*.mdb)&#125;; DBQ=" &amp; Server.MapPath ("counter.mdb"))<br>Connection.Open ConStr<br><br>' Create the record set and retrive the counter value<br>Set RS = Connection.Execute("SELECT * FROM count")<br><br>' Increase the counter value by one<br>Counts=RS("counter")+1<br><br>' Update the counter value in the database<br>Set RS = Connection.Execute ("UPDATE count SET counter =" &amp; Counts)<br>Connection.Close<br>%&gt;<br><br>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;TITLE&gt;Counter Example 2&lt;/TITLE&gt;<br>&lt;/HEAD&gt;<br>&lt;BODY&gt;<br>&lt;H1&gt;You are the visitor number &lt;%=Counts%&gt;&lt;/H1&gt;<br>&lt;/BODY&gt;<br>&lt;/HTML&gt;<br><br><br><br>What about Active users:<br><br>Some guys wants to know how many visitors are currently seeing the site, for that we will use another way, <br>we will create an application variable called active, and on each new session we will increase it by one <br>and when a session ends we will decrease it by one, and here is the code:<br><br>The Global.asa:<br>&lt;&#115cript LANGUAGE="VB&#115cript" RUNAT="Server"&gt;<br><br>Sub Application_OnStart<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Create the variable that will hold the number of active visitors<br>&nbsp;&nbsp;&nbsp;&nbsp;Application("Active") = 0<br>End Sub<br><br>Sub Session_OnStart<br>&nbsp;&nbsp;&nbsp;'Increase the counter by one<br>&nbsp;&nbsp;&nbsp;Application.Lock<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application("Active") = Application("Active") + 1<br>&nbsp;&nbsp;&nbsp;Application.UnLock<br><br>End Sub<br><br>Sub Session_OnEnd<br>&nbsp;&nbsp;&nbsp;' Decrease the c ounter<br>&nbsp;&nbsp;&nbsp;Application.Lock<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application("Active") = Application("Active") - 1<br>&nbsp;&nbsp;&nbsp;Application.UnLock<br>End Sub<br><br>&lt;/&#115cript&gt;<br><br><br>And to show the results just call the Application variable active in your web page just like this:<br>&lt;%= Application("Active")%&gt;<br><br><br><br><br>I hope you can create your own counters from now on. Give it try and good luck.<br></div> <div></div> </div><div id="Message" class="Message"></div></font></div> <script type="text/javascript"> document.body.oncopy = function () { setTimeout( function () { var text = clipboardData.getData("text"); if (text) { text = text + "\r\n本篇文章来源于 中国建站之家 原文链接:"+location.href; clipboardData.setData("text", text); } }, 100 ) } </script> <br> <a href="javascript:window.open('http://shuqian.qq.com/post?from=3&title='+encodeURIComponent(document.title)+'&uri='+encodeURIComponent(document.location.href)+'&jumpback=2&noui=1','favit','width=930,height=470,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes');void(0)" style="text-decoration:none;color:#155da5;display:block;background:url('http://shuqian.qq.com/img/add.gif') no-repeat 0px 0px;height:23px;width:300px;padding:2px 2px 0px 20px;font-size:14px;">将本文收藏到QQ书签与更多好友分享</a> <div></div> </td> <table width="575" border="0" cellpadding="0" cellspacing="0"> <tr> </tr> <tr> <td align="right" height=25 bgcolor=#F7F7F7 > <script language=javascript src=/wz/sg.JS></script> [<a href=javascript:window.print()>打 印</a>] </tr> </table> <table width="575" border="0" cellpadding="0" cellspacing="0"> <tr> </tr> <tr> <td align="right" height=25 bgcolor=#F7F7F7 style="display:block;padding:0px 10px"> <font color=#000000>[<script language=JavaScript src="/Article/Hits.Asp?ArticleID=5764"></script>]</font> [<a href="javascript:history.go(-1)">返回上一页</a>] [<a href="/user/favorite.asp?action=add&topic=用多种方法制作WEB页面的计数器">收 藏</a>]</td> </tr> <tr> <table width="575" border="0" cellpadding="0" cellspacing="0"> <tr> </tr> <tr> <td style="display:block;padding:0px 10px"><div><font color=#000000>上一篇文章:</font><a href=/Article/10/130/2005/200507205763.html>制作我们自己的Ebay(拍卖系统)(9)</a></div><div><font color=#000000>下一篇文章:</font><font color=#000000><a href=/Article/10/130/2005/200507205765.html>bbs树形结构的实现方法(一)</a></font></div></td> </tr> </table> <table width="575" border="0" cellpadding="0" cellspacing="0"> <tr> <td class="titlebg1">∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [<a href=/Article/comment.asp?ArticleID=5764 target=_blank>更多评论</a>...]</td> </tr> <tr valign="top"> <td></td> </tr> </table> </td> <td width="188" class="tableleft"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="titleback1">精彩推荐</td> </tr> <tr> <td height="260" valign="center" class="showbody1"><script language=javascript src=/ad/180601.js></script></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="titleback1">热门文章</td> </tr> <tr> <td height="100" valign="top" class="showbody1"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr> <td class="showlist11">· <a href='/Article/10/138/2005/200507256912.html' class="showlist" title="注册码大全二">注册码大全二</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/138/2005/200507256914.html' class="showlist" title="注册码大全四">注册码大全四</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/138/2005/200507256911.html' class="showlist" title="注册码大全一">注册码大全一</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/196/197/2005/2005081911736.html' class="showlist" title="要10G免费网络硬盘的请进来!">要10G免费网络硬盘的请进..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/206/2007/2007030319347.html' class="showlist" title="通过google 赶快来赚美金">通过google 赶快来赚美金..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/138/2005/200507256919.html' class="showlist" title="注册码大全十">注册码大全十</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/14/253/2005/2005092114218.html' class="showlist" title="头像-qq头像(qq新头像)4">头像-qq头像(qq新头像)4..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/13/150/2006/2006022316028.html' class="showlist" title="让你轻松架设FTP服务器1">让你轻松架设FTP服务器1..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/138/2005/200507256913.html' class="showlist" title="注册码大全三">注册码大全三</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/14/244/2005/2005092014121.html' class="showlist" title="梦幻背景图片7">梦幻背景图片7</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/14/249/2005/2005092114181.html' class="showlist" title="卡通动物图片6">卡通动物图片6</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/14/269/2005/2005092114241.html' class="showlist" title="网页制作素材-按钮素材2">网页制作素材-按钮素材2..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/13/150/2006/2006022316032.html' class="showlist" title="让你轻松架设FTP服务器5">让你轻松架设FTP服务器5..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/14/244/2005/2005092014153.html' class="showlist" title="风景图片8">风景图片8</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/138/2005/200507256918.html' class="showlist" title="注册码大全九">注册码大全九</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/13/150/2006/2006022316029.html' class="showlist" title="让你轻松架设FTP服务器2">让你轻松架设FTP服务器2..</a></td> <td class="showlist12"></td> </tr></table></td> </tr> <tr> <td height="2" bgcolor="#FFFFFF"></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="titleback1">关注此文读者还看过</td> </tr> <tr> <td height="100" valign="top" class="showbody1"><table width="100%" border="0" cellpadding="2" cellspacing="0"><tr> <td class="showlist11">· <a href='/Article/223/292/2007/2007091921983.html' target="_blank" class="showlist" title="四处惹祸不断 YouTube再度遭封杀">四处惹祸不断 YouTube再..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/130/2005/200507277793.html' target="_blank" class="showlist" title="asp性能测试报告(转)(三)">asp性能测试报告(转)(..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/133/2005/200507216833.html' target="_blank" class="showlist" title="泛珠之风需要强劲">泛珠之风需要强劲</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/131/2005/2005102114636.html' target="_blank" class="showlist" title="基于性能的编程技巧点滴">基于性能的编程技巧点滴..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/223/291/2006/2006011415439.html' target="_blank" class="showlist" title="欧洲搜索引擎Quarero始谋划 称Google难媲美">欧洲搜索引擎Quarero始谋..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/130/2005/2005121014917.html' target="_blank" class="showlist" title="Flash和Asp数据库的结合应用">Flash和Asp数据库的结合..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/137/2007/2007070720760.html' target="_blank" class="showlist" title="详细介绍优化mysql性能的十个参数">详细介绍优化mysql性能的..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/131/2007/2007091321867.html' target="_blank" class="showlist" title=".NET 3.x新特性之Lambda表达式">.NET 3.x新特性之Lambda..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/130/2005/2005091813541.html' target="_blank" class="showlist" title="制作我们自己的Ebay(拍卖系统EN) - Managing Bids - Page 5">制作我们自己的Ebay(拍卖..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/11/140/2006/2006013115773.html' target="_blank" class="showlist" title="Flash 8 少为人知的特性!(1)">Flash 8 少为人知的特性..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/133/2005/200507309363.html' target="_blank" class="showlist" title="聊天室php&amp;mysql(五)">聊天室php&amp;mysql(五..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/11/139/2005/200507194369.html' target="_blank" class="showlist" title="寻找Dreamweaver鲜为人知的小秘诀">寻找Dreamweaver鲜为人知..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/10/130/2005/200507288857.html' target="_blank" class="showlist" title="在ASP中利用COM组件开发Web应用程序">在ASP中利用COM组件开发..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/130/2005/200507288859.html' target="_blank" class="showlist" title="浅谈无刷新取得远程数据技术">浅谈无刷新取得远程数据..</a></td> <td class="showlist12"></td> </tr><tr> <td class="showlist11">· <a href='/Article/11/140/2005/2005081010627.html' target="_blank" class="showlist" title="Flash画面连结到不同的网页">Flash画面连结到不同的网..</a></td> <td class="showlist11"></td> </tr><tr> <td class="showlist12">· <a href='/Article/10/130/2007/2007022819289.html' target="_blank" class="showlist" title="身份证号码验证 -- (精华代码)">身份证号码验证 -- (精华..</a></td> <td class="showlist12"></td> </tr></table></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="titleback1">相关文章</td> </tr> <tr> <td height="100" valign="top" class="showbody1">· <a href="/Article/10/130/2005/2005091512716.html" title='LINE&nbsp;的计数器源程序(附源代码)'>LINE 的计数器源程序(附源..</a><br> · <a href="/Article/11/140/2005/2005081210934.html" title='flash版的计数器-制作方法'>flash版的计数器-制作方法</a><br> · <a href="/Article/10/133/2005/200507309458.html" title='php的计数器程序'>php的计数器程序</a><br> · <a href="/Article/10/133/2005/200507309437.html" title='最省空间的计数器'>最省空间的计数器</a><br> · <a href="/Article/10/133/2005/200507309408.html" title='简单易用的计数器(数据库)'>简单易用的计数器(数据库)..</a><br> · <a href="/Article/10/130/2005/200507288396.html" title='LINE 的计数器源程序(附源代码)'>LINE 的计数器源程序(附源..</a><br> · <a href="/Article/10/133/2005/200507216242.html" title='一个别致的计数器'>一个别致的计数器</a><br></td> </tr> </table> </td> </tr> </table> <table width="778" border="0" align="center" cellpadding="0" cellspacing="0" class="tableborder"> <tr> <td height="3"></td> </tr> </table> <table width="778" border="0" align="center" cellpadding="0" cellspacing="0" class="tableborder"> <tr> <td height="23" align="right"> <table width="100%" border="0" cellspacing="0" class="tablebody8" cellpadding="0"> <tr> <td height="5"></td> </tr> <tr> <td height="23" align="right" valign="middle"><center><a href="/support/about.asp" class="navmenu2">关于本站</a> - <a href="/support/help.asp" class="navmenu2">网站帮助</a> - <a href="/support/advertise.asp" class="navmenu2">广告合作</a> - <a href="/support/declare.asp" class="navmenu2">下载声明</a> - <a href="/link/" target="_blank" class="navmenu2">友情连接</a> - <a href="/support/sitemap.asp" class="navmenu2">网站地图</a> - <a href="#" target="_blank" class="navmenu2">人才招聘</a> </tr> </table></td> </tr> <tr> <td height="1"></td> </tr> </table> <table width="778" border="0" align="center" cellpadding="0" cellspacing="0" class="tableborder"> <tr> <td height="60" align="center" class="tablebody1">网站合作、内容监督、商务咨询:QQ: 9576619 <br> Copyright ? 2005--2008 中国建站之家版权所有 <br><a href="http://www.miibeian.gov.cn" target="_blank" title="粤ICP备05092265号"><font color=#000000>粤ICP备05092265号 </font><br><script src='http://s6.cnzz.com/stat.php?id=44148&web_id=44148&show=pic' language='JavaScript' charset='gb2312'></script> </td> <script language="javascript" src="/inc/Std_StranJF.Js"></script> </table> </body> </html> <span id="naruco_ad_body" style="display:none;"> <script language=javascript src=/adfile/top.js></script> </span> <script type="text/javascript"> var naruco_ad = document.getElementById('naruco_ad'); if (naruco_ad != null) { naruco_ad.innerHTML=naruco_ad_body.innerHTML; naruco_ad_body.innerHTML=""; } </script>