Using the Counters Object (1)

Source: Internet
Author: User
Tags count iis resource microsoft iis
Object 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 are only built for counting Page view hits. If we wanted to count something else, say all time a user entered a particular search query into our site ' s search page, We are hard pressed with using the PageCounter object. Fortunately Microsoft also provides developers with a more generic object, the counting object. This object can is used to track a indeterminent number of developer-defined counters.

Both the PageCounter and counters components are made with Microsoft IIS available Kit. Unfortunately there is shoddy documentation for the IIS Resource kit-it seems to being isntalled on IIS 5 (Windows watts) 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 R Esource 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 is able to create an instance of the Count ERs object using the following code:

<% Set obj = Server.CreateObject ("MSWC. Counters ")%>


If you have a Server.CreateObject Failed, Invalid class string error then the components are not properly. (If This is the case for 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's 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:

<object runat=server scope=application id=objcounter progid= "MSWC. Counters "></OBJECT>


To learn more about Global.asa is sure to read:everything your wanted to Know about Global.asa but were afraid Global.asa Events as creating a Global.asa File section of the sample chapter of Sams Teach yourself ASP 3.0 i N-days!

Once this and added to Global.asa can access the counters object from any ASP page into 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 ASP page on our Web site. We could use the counters object is much like we used the PageCounter object. For example, if we wanted to create a counter to?? Many folks visited we homepage we could use the following code O N Our homepage:

<%
' Increment the homepage counter
Objcounter.increment ("homepage")

' Display How many hits we ' ve had
Response.Write "There have been" & Objcounter.get ("homepage") & "hits."
%>




The Counter name homepage doesn ' t represent any special sort of name. Can simply create a counter by specifying a unique name. Once The counter is created to can increment it, change it, get its value, or remove it from any page on your Web Site. So, if in another page you had the same code as above and ten visitors hit your home page and then five visitors Second page, the value for the homepage counter would be 15-10 hits to 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 the various counters we may have created!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.