|
|
声明, 本菜鸟初学 c, 写得不好的地方请大哥您多指点.
说一下安装过程:
1. 即然是给 apache 写的模块, 那一定要装了 apache ~.-#
省略安装过程n行...
2. 然后把下面的代码存成 mod_pv.c
[php]/*
** mod_pv.c -- A Simple PageView Module for Apache
** author: lexrus via http://lex.w3.org.cn/
** version: 0.0.0-20050622
*/
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
#include "http_core.h"
#include "stdio.h"
#define LONG_LENGTH 20
//#define LOG_FILE "/var/log/apache2/mod_pageview"
#define LOG_FILE "/tmp/mod_pageview"
FILE *f;
char fc[LONG_LENGTH]; // File Content
long pv = 0; // PageView
static int pv_handler(request_rec *r)
{
if (strcmp(r->handler, "pv"))
return DECLINED;
if ((f = fopen( LOG_FILE, "r+")) == NULL)
f = fopen( LOG_FILE, "w");
fgets( fc, LONG_LENGTH, f );
pv = atol( fc ) + 1;
fseek( f, 0, 0 );
fprintf( f, "%d", pv );
fclose( f );
// r->content_type = "text/javascript";
if (!r->header_only)
ap_rprintf( r, "document.write(%d)", pv);
return OK;
}
static void pv_register_hooks(apr_pool_t *p)
{
ap_hook_handler(pv_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA pv_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
NULL, /* table of config file commands */
pv_register_hooks /* register hooks */
};[/php]
3. 编译咯
[php]apxs2 -i -c mod_pv.c[/php]
编译完以后 apxs2 会把 mod_pv.so 复制到 apache 的 modules 目录里, 不用管它.
4. 修改 apache 配置文件, 加载新模块, 配置访问路径
[php]LoadModule pv_module modules/mod_pv.so
...
<Location /pageview_script>
SetHandler pv
</Location>[/php]
5. 在你的网页里嵌入调用脚本
[php]My PageView: <script type="text/javascript" src="/pageview_script"></script>.[/php]
6. 重启 apache2
# apache2ctl restart
然后你访问你的网页, 就会看到这么一段: "My PageView: 1." |
|