新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > Asp教程 > 正文:Using the Counters Object (2)

Using the Counters Object (2)

新客网 XKER.COM 2003-07-11 来源: 收藏本文
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. 
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐