2011-12-01 41 views
1

我創建了一個簡單的Apache服務器模塊,遵循我在某處找到的示例hello world模塊。然後我添加了一個變量來跟蹤我的頁面上的訪問次數。這裏是我的模塊代碼:簡單的Apache服務器模塊

/* The simplest HelloWorld module */ 
#include <httpd.h> 
#include <http_protocol.h> 
#include <http_config.h> 

static int noOfViews = 0; 

static int helloworld_handler(request_rec *r) 
{ 
    noOfViews++; 

    if (!r->handler || strcmp(r->handler, "helloworld")) { 
     return DECLINED; 
    } 

    if (r->method_number != M_GET) { 
     return HTTP_METHOD_NOT_ALLOWED; 
    } 

    ap_set_content_type(r, "text/html;charset=ascii"); 
    ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n", 
      r); 
    ap_rputs("<html><head><title>Apache HelloWorld " 
      "Module</title></head>", r); 
    ap_rputs("<body><h1>Hello World!</h1>", r); 
    ap_rputs("<p>This is the Apache HelloWorld module!</p>", r); 
    ap_rprintf(r, "<p>Views: %d</p>", noOfViews); 
    ap_rputs("</body></html>", 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 
}; 

我的模塊目前遇到2個問題,我無法弄清楚。

  1. 我的觀點數似乎以2的倍數增加,即使我只希望它每次增加1。

  2. 當我不斷刷新我的頁面時,有時我的號碼會隨機下降。

有沒有人知道我的問題的根源是什麼?

謝謝你們這麼多!

回答

2
  1. 您正在遞增您實際無法處理的請求的計數器。

  2. Apache中的每個工作進程都有自己的副本noOfViews。無論您使用prefork還是worker MPM,這都適用;這只是prefork的一個明顯的問題。