title: Simple Apache Server Module
body: <p>I created a simple Apache server module following a sample hello world module I found somewhere. Then I added a variable that would track the number of visits on my page. Here is my module code:</p>
  
  <pre><code>/* The simplest HelloWorld module */
  #include &lt;httpd.h&gt;
  #include &lt;http_protocol.h&gt;
  #include &lt;http_config.h&gt;
  
  static int noOfViews = 0;
  
  static int helloworld_handler(request_rec *r)
  {
      noOfViews++;
  
      if (!r-&gt;handler || strcmp(r-&gt;handler, "helloworld")) {
          return DECLINED;
      }
  
      if (r-&gt;method_number != M_GET) {
          return HTTP_METHOD_NOT_ALLOWED;
      }
  
      ap_set_content_type(r, "text/html;charset=ascii");
      ap_rputs("&lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"&gt;\n",
               r);
      ap_rputs("&lt;html&gt;&lt;head&gt;&lt;title&gt;Apache HelloWorld "
               "Module&lt;/title&gt;&lt;/head&gt;", r);
      ap_rputs("&lt;body&gt;&lt;h1&gt;Hello World!&lt;/h1&gt;", r);
      ap_rputs("&lt;p&gt;This is the Apache HelloWorld module!&lt;/p&gt;", r);
      ap_rprintf(r, "&lt;p&gt;Views: %d&lt;/p&gt;", noOfViews);
      ap_rputs("&lt;/body&gt;&lt;/html&gt;", r);
      return OK;
  }
  
  static void helloworld_hooks(apr_pool_t *pool)
  {
      ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
  }
  
  module AP_MODULE_DECLARE_DATA helloworld_module = {
      STANDARD20_MODULE_STUFF,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
              helloworld_hooks
  };
  </code></pre>
  
  <p>My module is currently experiencing 2 problems that I am unable to figure out. </p>
  
  <ol>
  <li><p>My views count seems to go up in multiples of 2 even though I only want it to increment 1 at a time.</p></li>
  <li><p>When I constantly refresh my page, sometimes my number randomly falls.</p></li>
  </ol>
  
  <p>Does anyone know what the sources of my problems could be?</p>
  
  <p>Thank you guys so much!</p>