我根据在某处找到的示例 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 个我无法解决的问题。
我的观看次数似乎增加了 2 的倍数,尽管我只希望一次增加 1。
当我不断刷新页面时,有时我的数字会随机下降。
有谁知道我的问题的根源是什么?
非常感谢你们!
最佳答案
您正在为您实际未处理的请求递增计数器。
Apache 中的每个工作进程都有自己的
noOfViews
副本。无论您使用的是 prefork 还是 worker MPM,这都适用;这只是 prefork 的一个更明显的问题。
关于c - 简单的 Apache 服务器模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348571/