      $set preprocess(htmlpp) endp

      ******************************************************************
      *CGI Counter Demo
      *
      * This demo shows 2 counters:
      *
      * A Global count of the number of access to this program. This
      *  uses a file to record the access count to date.
      * A Browser local counter, giving the number of times the current
      *  Browser has accessed this program. A Browser COOKIE is used
      *  to pass state between the Browser and this program, to keep
      *  track of the Browser's access count.
      *
      *  The ACCEPT external-form-name syntax has been enhanced to
      *  read all the cookie name=value pairs send to the browser.
      ******************************************************************

       program-id. "howmany".

      *> File to keep global counter in
       select optional F1 assign "counter.dat"
           organization is relative
           access mode is random
           relative key is CountKey
           file status is FileStatus
           lock mode is exclusive.

       file section.
       fd  f1
       record contains 9 characters.
       01  f1-Rec.
           03  CountValue pic 9(9).

       working-storage section.
       01  Counter      pic z(8)9.       *> global counter
       01  MyCounter    pic z(8)9.       *> local to browser counter
       01  EnvValue     pic x(256).      *> environment variable value
       01  Cnt          pic 9(9) comp-5. *> temp counter
       01  Buff         pic x(40).       *> temp buffer
       01  FileStatus   pic xx.          *> status for counter file
       01  CountKey     pic 9 value 1.   *> 1st record is one we want

       01  Browser-cookie is external-form.
           03  CookieValue pic x(40) identified by "COBOLCookie".
       procedure division.
       main section.

      *Get the global counter from file
       open i-o F1
       if FileStatus = "05"
       then
           move 1 to CountValue     *> init global counter
           write f1-rec
           move 1 to Counter
       else
           read F1
           add 1 to CountValue      *> increment counter
           rewrite f1-rec
           move CountValue to Counter
       end-if
       close F1

      *Search for our Cookie from Browser (its name is "COBOLCookie")
       accept Browser-cookie

      *CookieValue now contains the Counter for local browser

      *Increment the local browser's counter
       move function numval(CookieValue) to cnt
       add 1 to cnt giving MyCounter

      *Output HTML header back to browser (including cookie)
       exec html
           <HTML>
           <HEAD>
           <TITLE>Counter Example</TITLE>
           <META HTTP-EQUIV="Set-Cookie"
           CONTENT="COBOLCookie=:MyCounter">
           </HEAD>
       end-exec



      *Output HTML body back to browser
       exec html
           <BODY>
       end-exec

      *Use COBOL logic to customize the page output
       if CountValue = 1
       then
           exec html
               <H1>This is the first time this page has
               been accessed.</H1>
               <P>
               (hit reload to access this page again)
           end-exec
       else
           exec html
               <H1>This page has been accessed :Counter times.</H1>
               <P>
               <H1>You have accessed this page :MyCounter times.</H1>
               (You can prove it by hitting reload!)
           end-exec
       end-if
       exec html
           </BODY>
           </HTML>
       end-exec

       exit program

       end program "howmany".

