home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


HitCount

The HitCount program creates that all-time favorite, a page’s hit count. The output is a cardinal number (1, 2, 3, and so on) and nothing else. HitCount works only on Windows NT. See Listing 32.5 for the C source code.

Listing 32.5 hitcount.c—A Counter Script Used as an SSI


// HITCOUNT.C
// This SSI program produces a cardinal number page hit
// count based on the environment variable SCRIPT_NAME.

#include <windows.h>
#include <stdio.h>
#define      ERROR_CANT_CREATE “HitCount:  Cannot open/create
⇒registry key.”
#define  ERROR_CANT_UPDATE “HitCount:  Cannot update registry key.”
#define  HITCOUNT “Software\\Greyware\\HitCount\\Pages”

void main(int argc, char * argv[]) {
     char     szHits[33];     // number of hits for this page
     char     szDefPage[80];  // system default pagename
     char     *p;             // generic pointer
     char     *PageName;      // pointer to this page’s name
     long     dwLength=33;    // length of temporary buffer
     long     dwType;         // registry value type code
     long     dwRetCode;      // generic return code from API
     HKEY     hKey;           // registry key handle

     // Determine where to get the page name.  A command-
     // line argument overrides the SCRIPT_NAME variable.

     if ((argc==2) && ((*argv[1]==‘/’) | (*argv[1]==‘\\’)))
          PageName = argv[1];
     else
          PageName = getenv(“SCRIPT_NAME”);

     // If invoked from without SCRIPT_NAME or args, die

     if (PageName==NULL)
     {
          printf(“HitCount 1.0.b.960121\n”
                 “Copyright (c) 1995,96 Greyware ”
                 “Automation Products\n\n”
                 “Documentation available online from “
                 “Greyware’s Web server:\n”
                 <“http://www.greyware.com/”>
                 “greyware/software/freeware.htp\n\n”);
     }
     else
     {

          // Open the registry key

          dwRetCode = RegOpenKeyEx (
               HKEY_LOCAL_MACHINE,
               HITCOUNT,
               0,
               KEY_EXECUTE,
               &hKey);

          // If open failed because key doesn’t exist,
          // create it

          if ((dwRetCode==ERROR_BADDB)
               || (dwRetCode==ERROR_BADKEY)
               || (dwRetCode==ERROR_FILE_NOT_FOUND))
               dwRetCode = RegCreateKey(
                    HKEY_LOCAL_MACHINE,
                    HITCOUNT,
                    &hKey);

          // If couldn’t open or create, die

          if (dwRetCode != ERROR_SUCCESS) {
               printf (ERROR_CANT_CREATE);

          } else {

               // Get the default page name

               dwLength = sizeof(szDefPage);
               dwRetCode = RegQueryValueEx (
                    hKey,
                    “(default)”,
                    0,
                    &dwType,
                    szDefPage,
                    &dwLength);

               if ((dwRetCode == ERROR_SUCCESS)
                    && (dwType == REG_SZ)
                    && (dwLength > 0)) {
                    szDefPage[dwLength] = ‘\0’;
               } else {
                    strcpy(szDefPage,“default.htm”);
               }

               // If current page uses default page name,
               // strip the page name

              _strlwr(PageName);
               p = strrchr(PageName,‘/’);
               if (p==NULL) p = strrchr(PageName,‘\\’);
               if (p) {
                    p++;
                    if (stricmp(p,szDefPage)==0) *p = ‘\0’;
               }

               // Get this page’s information

               dwLength = sizeof(szHits);
               dwRetCode = RegQueryValueEx (
                    hKey,
                    PageName,
                    0,
                    &dwType,
                    szHits,
                    &dwLength);

               if ((dwRetCode == ERROR_SUCCESS)
                    && (dwType == REG_SZ)
                    && (dwLength >0)) {
                    szHits[dwLength] = ‘\0’;
               } else {
                    strcpy (szHits, “1”);
               }

               // Close the registry key

               dwRetCode = RegCloseKey(hKey);

               // Print this page’s count

               printf(“%s”,szHits);

               // Bump the count by one for next call

              _ltoa ((atol(szHits)+1), szHits, 10);

               // Write the new value back to the registry

               dwRetCode = RegOpenKeyEx (
                    HKEY_LOCAL_MACHINE,
                    HITCOUNT,
                    0,
                    KEY_SET_VALUE,
                    &hKey);

               if (dwRetCode==ERROR_SUCCESS) {
                    dwRetCode = RegSetValueEx(
                         hKey,
                         PageName,
                         0,
                         REG_SZ,
                         szHits,
                         strlen(szHits));
                    dwRetCode = RegCloseKey(hKey);
               } else {
                    printf(ERROR_CANT_UPDATE);
               }
          }
     }
     fflush(stdout);
     return;
}

HitCount takes advantage of one of NT’s unsung glories, the system Registry. Counters for other platforms need to worry about creating and updating a database file, file locking, concurrency, and a number of other messy issues. HitCount uses the hierarchical Registry as a database, letting the operating system take care of concurrent access.

HitCount is actually remarkably simple compared to other counters. It uses the SCRIPT_NAME environment variable to determine the name of the current page. Therefore, you have no worries about passing unique strings as parameters. HitCount takes the page name and either creates or updates a Registry entry for it. The information is always available and can be rapidly accessed.

HitCount works on most NT servers. One notable exception is WebSite. WebSite supplies the SCRIPT_NAME variable, but also supplies spurious arguments in the argv[] array. To make HitCount work with WebSite, delete the section of code that checks for command-line arguments.

HitCount, like the other samples in this chapter, is freeware from Greyware Automation Products (http://www.greyware.com/). You can find more extensive documentation online at its site. The code is unmodified from the code distributed by Greyware for a good reason: Registry keys are named, so having multiple versions of the software running around loose with different key names just wouldn’t do. Therefore, the code retains the key names for compatibility.

The only bit of configuration you might need to do is if your server’s default page name isn’t default.htm. In that case, add this key to the Registry before using HitCount for the first time:

HKEY_LOCAL_MACHINE
    \Software
        \Greyware
            \HitCount
                \Pages


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.