Jan 7

apache, mod_cgi 不指定

felix021 @ 2010-1-7 20:40 [IT » 网络] 评论(1) , 引用(0) , 阅读(6545) | Via 本站原创 | |
编译apache的时候默认应该是有mod_cgi的,如果不确定,可以加上--enable_cgi。

在httpd.conf里面增加(或修改,因为可能已经有这个东西了):
引用
ScriptAlias /cgi-bin/ /某路径/cgi-bin/
如果你修改了“某路径”,那么后面对应的Directory一节也应该对应修改
引用
<Directory "/某路径/cgi-bin">                                                  
    AllowOverride None
    Options FollowSymLinks #如果不放心,这里还可以加上一个ExecCgi选项。
    Order allow,deny
    Allow from all
</Directory>

下面是一个简单的C编写的cgi程序,gcc -o printall simple_cgi.c,把printall放在cgi-bin目录下面。然后访问:http://localhost/cgi-bin/printall,可以看到输出。特别注意一下,cgi不仅要打HTTP实体,还要打HTTP头信息。如果你实在懒得写,在实体前面加上一个回车就行了:) 否则你会看到apache的error_log里输出诸如"malformed header from script. Bad header="这样的错误信息。
#include <stdio.h>
#include <stdlib.h>

extern char ** environ;

int main ()
{
    int i;
    puts("Content-Type: text/plain\n\n");
    for (i = 0; environ[i] != NULL; i++) {
        printf ("%s\n", environ[i]);
    }

    if (strncmp("POST", getenv("REQUEST_METHOD"), 4) == 0) {
        char buf[1025];
        while (1) {
            fgets(buf, 1024, stdin);
            printf( "%s", buf);
            if (feof(stdin)) {
                break;
            }
        }
    }
    return 0;
}

特别点出一下,GET请求的query_string是在environ["QUERY_STRING"]里面,而POST请求的输入,是从stdin中读取。



欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
ivan Homepage
2010-1-11 07:04
你想说什么嘞?
felix021 回复于 2010-1-11 12:47
没什么  我就记录一下
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]