LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2487|回复: 2

刚写的 mod_pv.c 可以帮你统计 PageView 哦

[复制链接]
发表于 2005-6-22 23:01:49 | 显示全部楼层 |阅读模式
声明, 本菜鸟初学 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."
 楼主| 发表于 2005-6-22 23:05:56 | 显示全部楼层
忘了说了, 可以用 apxs2 -g -n hello 来建立 hello world 程序, 安装过程和上面说的差不多.

另外附上: Apache Web server 1.3 API Dictionary
http://httpd.apache.org/dev/apidoc/
回复 支持 反对

使用道具 举报

发表于 2005-6-23 21:46:07 | 显示全部楼层
不错,有参考价值。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表