Counters Component

The Counter component creates a Counters object that can create, store, increment, and retrieve any number of individual counters.

A counter is a persistent value that contains an integer. You can manipulate a counter with the Get, Increment, Set, and Remove methods of the Counters object. Once you create the counter, it persists until you remove it.

Counters do not automatically increment on an event like a page hit. You must manually set or increment counters using the Set and Increment methods.

Counters are not limited in scope. Once you create a counter, any page on your site can retrieve or manipulate its value. For example, if you increment and display a counter named hits in a page called Page1.asp, and you increment hits in another page called Page2.asp, both pages will increment the same counter. If you hit Page1.asp, and increment hits to 34, hitting Page2.asp will increment hits to 35. The next time you hit Page1.asp, hits will increment to 36.

All counters are stored in a single text file, counters.txt, which is located in the same directory as the counters.dll file.

File Names

counters.dll The Counters component.
counters.txt The file that stores all individual counters on a site. counters.txt is a UTF8-encoded file. You can have any Unicode characters in a counter name.

Syntax

Create the Counters object one time on your server by adding the following to the global.asa file:

<OBJECT 
RUNAT=Server 
SCOPE=Application 
ID=Counter 
PROGID="MSWC.Counters"> 
</OBJECT>

Registry Entries

None.

Remarks

Only create one Counters object in your site. This single Counters object can create any number of individual counters.

Note  For Personal Web Server on Windows?95, a Counters component has already been specified in the global.asa file in the default virtual directory. You can work with the Counters object the component creates as if it were a built-in object by calling Counters.Get, Counters.Increment, Counters.Remove, and Counters.Set . You should not create another instance of the Counters object.

Methods

Get Returns the value of the counter.
Increment Increases the counter by 1.
Remove Removes the counter from the counters.txt file.
Set Sets the value of the counter to a specific integer.

Example

Create an instance of the Counters object in the global.asa file with the ID attribute set to Counter:

<OBJECT RUNAT=Server SCOPE=Application ID=Counter PROGID="MSWC.Counters"> </OBJECT>
 

You can then use that Counters object on one page to create all the counters you need:

There have been <%= Counter.Increment('defaultPageHits') %> to this site.
 

Then on another page you can increment the counter in the following manner:

You are visitor number<%= Counter.Increment('LinksPageHits') %> to this page.
 

Note  You can obtain this component from the CD included with the IIS Resource Kit.