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


After you create the key, add a value under Pages. The name of the value is (default) and its type is REG_SZ. Fill in the name of your system’s default page. Case doesn’t matter.

HitCount uses this information to keep from falsely distinguishing between a hit to http://www.yourserver.com/ and one to http://www.yourserver.com/default.name. Some Web servers would report these two as different URLs in the SCRIPT_NAME environment variable, even though they refer to the same physical page. By setting the default in the registry, you tell HitCount to strip the page name off, if found, thus reconciling any potential problems before they arise. The default is default.htm, so you need to set this value only if your SSI pages use a different name.

HitCntth

HitCntth is a variation of HitCount. Its output is an ordinal number (1st, 2nd, 3rd, and so on). You probably understand the name by now. HitCntth provides the HitCount-th number. Get it?

HitCntth is designed to work alongside HitCount. It uses the same Registry keys, so you can switch from one format to the other without having to reset the counter or worry about duplicate counts. See the HitCount documentation for configuration details.

Creating an ordinal takes a bit more work than printing a cardinal number because the English method of counting is somewhat arbitrary. HitCntth looks for exceptions and handles them separately, and then throws a th on the end of anything left over. Otherwise, the function is identical to HitCount. Listing 32.6 shows the source code for HitCntth.

Listing 32.6 hitcntth.c—A Counter Script that Provides the Count Using Ordinal Numbers


// HITCNTTH.C
// This SSI program produces an ordinal number page hit
// count based on the environment variable SCRIPT_NAME.

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

void main(int argc, char * argv[]) {
     char     szHits[36];      // 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=36;     // 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(“HitCntth 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\0”);
               }

               // Close the registry key

               dwRetCode = RegCloseKey(hKey);

               // Check for special cases:
               // look at count mod 100 first

               switch ((atol(szHits)) % 100) {
                    case 11:     // 11th, 111th, 211th, etc.
                         printf(“%sth”,szHits);
                         break;
                    case 12:     // 12th, 112th, 212th, etc.
                         printf(“%sth”,szHits);
                         break;
                    case 13:     // 13th, 113th, 213th, etc.
                         printf(“%sth”,szHits);
                         break;
                    default:
                         // no choice but to look at last
                         // digit
                         switch (szHits[strlen(szHits)-1]) {
                              case ‘1’:     // 1st, 21st, 31st
                                   printf(“%sst”,szHits);
                                   break;
                              case ‘2’:     // 2nd, 22nd, 32nd
                                   printf(“%snd”,szHits);
                                   break;
                              case ‘3’:     // 3rd, 23rd, 36rd
                                   printf(“%srd”,szHits);
                                   break;
                              default:
                                   printf(“%sth”,szHits);
                                   break;
                         }
               }
               // 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;
}


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.