我要投稿

Using the Counters Object (1)

时间:2010-08-13 09:34 作者:cccbbs 阅读:1154

扫描二维码关注公众号,可以查询淘宝、京东商品优惠券

            
                                  

[/td]
            
Introduction:

In yesterday's article, Recording Page Hits with Microsoft's Page Counter Object we examined using Microsoft's PageCounter object to track the number of hits for any particular Web page. Unfortunately this component is only built for counting page view hits. If we wanted to count something else, say each time a user entered a particular search query into our site's search page, we'd be hard pressed with using the PageCounter object. Fortunately Microsoft also provides developers with a more generic counting object, the Counters object. This object can be used to track an indeterminent number of developer-defined counters.



Both the PageCounter and Counters components are made available with Microsoft's IIS Resource Kit. Unfortunately there is shoddy documentation for the IIS Resource Kit - it seems to be isntalled on IIS 5 (Windows 2000) by default, but older versions of IIS may not have it properly installed. Refer to the previous article, Recording Page Hits with Microsoft's Page Counter Object, for more information on the IIS Resource Kit and how to obtain it if you don't have it installed.



Assuming you have the IIS Resource Kit components properly installed you should be able to create an instance of the Counters object using the following code:



??





If you get a Server.CreateObject Failed, Invalid class string error then the components are not properly installed. (If this is the case you should try changing MSWC.Counters to IISSample.Counters and cross your fingers.) If you receive no such error message then the component is properly installed.



Using the Counters Object:

The Counters object allows you to create any number of persistent counters. When using the Counters object it is smartest to create a sinlge application-wide instance of this object. We can most easily do this by adding the following line to the top of our Global.asa file&58



??





To learn more about Global.asa be sure to read: Everything you Wanted to Know about Global.asa but were afraid to Ask and Global.asa Events as well as the Creating a Global.asa File section of the sample chapter of Sams Teach Yourself ASP 3.0 in 21 Days!



Once this line is added to Global.asa you can Access the Counters object from any ASP page in your Web site by the object's ID that we specified in the OBJECT tag: objCounter. Like the PageCounter object discussed in yesterday's article, the Counters object is a fairly simple object consisting of only four methods:





Get(CounterName) - Retrieves the current value of the counter specified by the name CounterName



Increment(CounterName) - Increments the current value of the counter specified by the name CounterName



Remove(CounterName) - Removes the counter specified by the name CounterName from the text file counters.txt. This text file is where the counter information is persisted.



Set(CounterName, Value) - Sets the value of the counter specified by the name CounterName to the value specified by Value

So, once we have added the OBJECT tag above to our Global.asa file we can start working with the Counters object from any ASP page on our Web site. We could use the Counters object much like we used the PageCounter object. For example, if we wanted to create a counter to see how many folks visited our homepage we could use the following code on our homepage:













The counter name HomePage doesn't represent any special sort of name. You can simply create a counter by specifying a unique name. Once the counter is created you can increment it, change it, get its current value, or remove it from any page on your Web site. So, if on another page you had the same code as above and ten visitors hit your home page and then five visitors hit this second page, the value for the HomePage counter will be 15 - 10 hits from the homepage and 5 hits from the secondary page.



In Part 2 we'll examine the Counters object in more detail, looking at how and where its values are persisted. We'll also look at some code to display all of the values of the various counters we may have created!

最新评论

我要投稿 我要评论
插入url链接 添加表情
限 100000 字节
响应相关主管单位规定,关闭回帖发帖功能
[VB毕业设计] [ASP毕业设计] [JAVA毕业设计] [JSP毕业设计] [PHP毕业设计] [asp.net毕业设计] [.net毕业设计] [网络毕业设计] [Delphi毕业设计] [VC毕业设计] [VF毕业设计] [机械毕业设计] [工艺毕业设计] [模具毕业设计] [其他毕业设计]
站长联系 cccbbsnet#163.com 本站系公益性质网站,站内广告维系本站运作。欢迎赞助本站。
大学生计算机相关方向|软件编程|网络工程|web开发|数据库技术|学习资料等
本社区仅供学习计算机相关技术所使用,访客发言不得违反国家法律法规
Time now is:10-19 07:03, Gzip enabled 蜀ICP备07004838号Powered by cccbbs.net v7.5 SP3 Code © 2003-06 cccbbs
计算机毕业设计