|
CHAPTER 20
Page Counter Component |
| There was a time that Im sure many of you remember (not that long ago) when a page counter on a web sites home page was a novelty. Back then, adding a page counter requiredor so it seemedfar more work than it was worth, involving at least a CGI application or maybe a Java applet. The alternative was easier but fraught with difficulties of its own. It involved using a counter service. This involved adding an <IMG> tag to your site that referenced a CGI application on the counter service-maintained server. The service maintained the counter for you. The problem was that such services often were out of commission for long periods of time and would go down completely under heavy loads. Because counter services were problematic, many developers decided to create their own, often simply reinventing the wheel. Now, however, the web is beginning to show signs that it has moved from infancy to its toddler years, and such mundane items as page counters have become everyday occurrences. There are now at least a dozen easily obtainable versions of the ever-present page counter. Microsoft has its own version; its Page Counter component is the topic of this chapter. Microsofts version of the Page Counter is a simple component that stores the current page count for a specific page to a text file. Code on your active server page increases the counter and retrieves the current count programmatically through calls to methods of the Page Counter object. This chapter documents the Page Counter component 2.0 (Beta 3), which is available for download from Microsofts web site. Instantiating the Page Counter Component 355 Page Counter Instantiating the Page Counter Component ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition Copyright © 2000 OReilly & Associates, Inc. All rights reserved. Accessory Files/Required DLL Files pagecnt.dll The dynamic link library for the Page Counter component. This DLL comes with the IIS installation media but is not installed by default. You must register this DLL by hand before you can use it. Hit Count Data File The hit count data file contains the current hit count for every page for which the Page Counter object is being used. Microsoft suggests that you do not modify this file by hand. However, doing so does not adversely affect the page counters functionality unless the format of the entries is changed. The name and location of this file is specified by the File Location value entry in the registry key HKEY_CLASSES_ROOT\MSWC.PageCounter. The default name for this page count file is hitcnt.cnt. Note that the Page Counter object will save the current hit count for a page if the count rises above a certain number. This number is located in the Save_Count value (under the same registry key as the File_Location value). The default number for this setting is 25. Instantiating the Page Counter Component To create an object variable containing an instance of the Page Counter component, use the CreateObject method of the Server object. The syntax for the CreateObject method is as follows: Set objMyObject = Server.CreateObject(strProgId) where: objMyObject represents the name of a Page Counter object. strProgId represents the programmatic identifier (ProgID) for the Page Counter component; its ProgID is IISSample.PageCounter. Page Counter Summary Properties None Collections None Methods Hits Reset Events None 356 Chapter 20 Page Counter Component Comments/Troubleshooting ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition Copyright © 2000 OReilly & Associates, Inc. All rights reserved. Example <% ' The following code uses the CreateObject method of the ' Server object to instantiate a Page Counter object on ' the server. Dim objPgCounter Set objPgCounter = _ Server.CreateObject("IISSample.PageCounter") %> For more details on the use of the CreateObject method see its documentation in Chapter 8, Server Object. Comments/Troubleshooting The Page Counter component uses an internal object called a Central Management object that is part of the IIS architecture. This object is what actually counts the number of times each page has been hit. What if you want to create an application-wide counter, rather than just a pagelevel counter? Unfortunately, the Page Counter component cannot help you. You must use either the Counters component or an application-scoped variable that is saved on the system manually. The other limitation of the Page Counter component is that there is no way to prevent the page count from being artificially incremented by the users clicking the Refresh button or reloading the page repeatedly. The Page Counter component is simple to use and works as documented. Methods Reference Hits objPgCntr.Hits([strPathInfo]) Retrieves a Long from the Page Counter hits file representing the total number of times a given page has been requested. Parameters strPathInfo The virtual path and filename for the page whose hit count you wish to retrieve. If you do not include a strPathInfo argument, the Page Counter object will retrieve the number of times the current page has been requested. Example <% ' Declare local variables. Reset 357 Page Counter Reset ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition Copyright © 2000 OReilly & Associates, Inc. All rights reserved. Dim objPgCntr Dim lngHitCount ' Instantiate a Page Counter object. Set objPgCntr = Server.CreateObject( _ "IISSample.PageCounter") ' Retrieve the hit count for the home page. lngHitCount = objPgCntr.Hits("/Apps/Homepage.asp") %> The home page has been served <%= lngHitCount %> times. Notes As explained earlier, a pages hit count is updated automatically (assuming that page contains a Page Counter object) any time a user requests it. This number shows both new requests and those produced from simply clicking on the Refresh button. Reset objPgCntr.Reset([strPathInfo]) Resets the page counter for a web page. Once called, the page count for the page is reset to zero in the Page Count hits file. Parameters strPathInfo A string value that represents the virtual path and filename for the page whose hit count you wish to reset. If you do not include a strPathInfo argument, the Page Counter object will reset the count for the current page to zero. Example <% ' Declare local variables. Dim objPgCntr Dim lngHitCount ' Instantiate a Page Counter object. Set objPgCntr = Server.CreateObject( _ "IISSample.PageCounter") ' Reset the hit count for the home page. objPgCntr.Reset("/Apps/Homepage.asp") ...[additional code] %> Notes If the hits file becomes corrupted or is deleted, the hit counts for all pages are essentially reset. 358 ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition Copyright © 2000 OReilly & Associates, Inc. All rights reserved. MyInfo component is a string that reflects whether or not this Messages form should appear on your home page. The value is by default (before you build your web page using the wizard), 1 if you have chosen to have the Messages form, and 0 if you have chosen not to have the Messages form. OrganizationAddress A string representing the address of the web sites organization. OrganizationName A string representing the name of the web sites organization. OrganizationPhone A string representing the phone number of the web sites organization. OrganizationWords Any additional text associated with the web sites organization. PageType This property is also a reflection of information you choose through the use of the Personal Web Server Home Page Wizard. However, it is from the older version (3.0) of PWS and is not the Home Page wizard for PWS 4.0. This propertys value is a number that represents whether the current site is (1) About My Company, (2) About My Life, (3) About My School, (4) About My Organization, or (5) About My Community. PersonalAddress* A string representing the address of the web sites owner. PersonalMail* The email address of the web sites owner. PersonalName* A string representing the name of the web site owner. PersonalPhone* A string representing the phone number of the web sites owner. PersonalWords* The additional text associated with the web sites owner. SchoolAddress The address of the web sites school. SchoolDepartment The department of the web sites school. SchoolName A string representing the name of the web sites school. SchoolPhone The phone number of the web sites school. SchoolWords A string representing any additional text associated with the web sites school. Style A string representing the relative URL of a style sheet for the web site. Table 19-1: Property Name and Description (continued) Property Name Description 352 Chapter 19 MyInfo Component [All Properties] ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition Copyright © 2000 OReilly & Associates, Inc. All rights reserved. strPropertyValue A string that represents the new value for a given property. If the property name does not exist, it is created and initialized with the value of strPropertyValue. Example The following example code demonstrates both the instantiation of a MyInfo object and its use. First, a MyInfo object named appMyInfo is instantiated in GLOBAL.ASA: [FROM GLOBAL.ASA] <% ' Declare local variables. Dim appMyInfo ' Instantiate a MyInfo object with application-level scope. Set Application("appMyInfo") = _ Server.CreateObject("MSWC.MyInfo") ...[additional code] %> The following is from elsewhere in the ASP application and shows how to assign values to and retrieve values from the MyInfo object: <% ' You can set the default values. Application("appMyInfo").PersonalName = _ "A. Keyton Weissinger" ' You can also create (or set) new values. Application("appMyInfo").MyNewProp = _ "Custom Property Value" ...[additional code] ' Now you can use these values as you would any other ' application-level values. %> The value of the PersonalName property is <%= Application("appMyInfo").PersonalName %><BR> Title A string representing the user-defined title for the home page. URL(N) A string representing the Nth user-defined URL. This collection allows you to store multiple user-defined URLs for easy access. URLWords(N) A string representing the description of the Nth userdefined URL. This collection allows you to store the descriptions for the URLs in the URL collection. Table 19-1: Property Name and Description (continued) Property Name Description [All Properties] 353 MyInfo [All Properties] ASP in a Nutshell: A Desktop Quick Reference, eMatter Edition Copyright © 2000 OReilly & Associates, Inc. All rights reserved. The value of the MyNewProp property is <%= Application("appMyInfo").MyNewProp %><BR> Notes The only properties whose values are in any way unusual are the URL and URLWords collections. These allow you to create a collection of URLs for later use in your site. The following demonstrates the use of these properties: <% ' Set the URL for the first URL in the collection. Application("appMyInfo").URL(1) = _ "/Apps/HomeDir/Home.asp" ' Set the description for the first URL in the ' collection. Application("appMyInfo").URLWords(1) = _ "My Site's Home Page" . . . ' Now you can use these values to create a link (with a ' descriptive name) to a particular URL. %> <A HREF = "Application("appMyInfo").URL(1)"> <%=Application("appMyInfo").URLWords(1)%> </A> |