我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > ASP专区 > Asp技巧/优化 > Using the Counters Object (2)
热门文章排行
热门文章排行 手推车”功能的实现(10-07)
八大法则防范ASP网站漏洞(10-23)
ASP教程十一、调试ASP脚本(10-23)
在JSP中访问数据库大全(10-23)
虚机服务中常见Asp.Net低级错误一览(03-21)
精采文章排行
精采文章排行 ASP.NET实现抓取网页中的链接(11-15)
ASP连接数据库的11种方法(11-10)
如何动态创建网页的RSS内容摘要(11-10)
ASP网站漏洞及入侵防范方法(11-10)
ASP自定义函数:对字符串正则替换(11-10)
技术专题推荐
网管论坛交流
 

Using the Counters Object (2) 

作者:佚名   来源:一亩三分地   点击:   日期:2007-03-22

In Part 1 we looked at the Counters object and what purpose it serves; we also examined its four methods. In this part we're going to look at the Counters object in a bit more depth and look at some code to track the popularity of various search terms and code to display the values of all of the counters!

Persisting Counter Data:
The Counters object persists its various counters' information. That is, even if the Web server is rebooted the value in these counters will not be reset back to zero. This is possible because the data for each counter is stored in a text file, counters.txt. This file will most likely be located in \WINNT\system32\ or \WINNT\system32\inetsrv\Data\. The file's structure is pretty straight foward. Each counter that you create has its own line in the text file. On each line the name of the counter comes first, followed by a colon, followed by the value of the counter. For example:


HomePage:1935
SomeOtherPage:234
Product Queries:45
Advertisement Displays:35221

Therefore, if we wanted to display the values of all of our counters in an ASP page we could use some very simple FileSystemObject code to open this file and read the contents of each line, displaying the name of the counter and its value. The function presented below can be cut and pasted into your application to list the contents of each counter.

Function DisplayCounters()
  'The full physical filename of the counters text file
  Const strFileName = "C:\WINNT\system32\inetsrv\Data\counters.txt"

  Dim objFSO, objTS
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

  Set objTS = objFSO.OpenTextFile(strFileName)

  'Read in the lines one at a time
  Dim strLine
  Do While Not objTS.AtEndOfStream

    'Read in the line from the file
    strLine = objTS.ReadLine()
    
    'Display the counter name and value
    Response.Write split(strLine,":")(0) & " - " & _
                   FormatNumber(split(strLine, ":")(1), 0) & "<br>"

  Loop

  'Clean up...
  objTS.Close
  Set objTS = Nothing
  Set objFSO = Nothing
End Function




With a little more work you could read these values into a two-dimensional array and then sort the array in descending order by the counter values. Then you could selectively display, say, the top ten counters or whatnot. If this interests you be sure to check out: Sorting a Two-Dimensional Array with BubbleSort.

Tracking the Popularity of Various Search Terms:
One very useful application for the Counters object would be to track the popularity of various search terms entered by your users. For example, if you have a search engine on your site (like the search engine here at 4Guys) you may be curious what search keywords are the most popular. To track this metric, all you would need to do is add the following code to the search page:

  ...

  'This assumes that the user's search term was passed through the
  'querystring as "SearchTerm"
  Dim strSearchTerm
  strSearchTerm = Request.QueryString("SearchTerm")

  'If the user entered a search term than increment the counter  
  If Len(strSearchTerm) > 0 then
    objCounter.Increment(strSearchTerm)
  End If
  
  ...




Then, in some reporting screen, you can use the DisplayCounters() function to list the various search terms and their associated popularity.

Conclusion:
This wraps up our examination of the Counters object. The Counters object is a more powerful version of the PageCounter object, capable of providing multiple counters across your entire Web site. 

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·ASP中巧用Response属性 ·第六课:ASP脚本循环语句
·在 Web 页上使用条件数值格式 ·连接数据库查询手册(不仅仅适用于asp)
·警惕"给你的FileSystemObject对象加把锁" ·用ASP做全文检索

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。