Apache 上的 CGI 配置与测试

Apache 上的 CGI 配置与测试

平台与工具:

        Windows XP, Apache 2.0.59, ActivePerl-5.6.1.628-MSWin32-x86-multi-thread.msi, VC++ 6.0, Turboc 2.0

一、apache 的配置文件“http.conf ”与CGI相关的配置:

        ScriptAlias /cgi-bin/ "f:/php_code/cgi-bin/"

        <Directory "f:/php_code/cgi-bin/">
            AllowOverride All
           Options ExecCGI
           Order allow,deny
           Allow from all
        </Directory>

        AddHandler cgi-script .cgi .pl

        先停止apache服务,保存http.conf文件配置,再启动服务。

二、perl脚本CGI:

        注意perl脚本的首句内容要给出perl解释器的路径,如果perl和apache不是安装在同一目录,要给出绝对路径。如下示例:

        #!F:/Perl/bin/perl.exe
        ##
        ##  printenv -- demo CGI program which just prints its environment
        ##
        print "Content-type: text/html/n/n";
        foreach $var (sort(keys(%ENV))) {
            $val = $ENV{$var};
            $val =~ s|/n|//n|g;
           $val =~ s|"|//"|g;
           print "${var}=/"${val}/"<br/>/n";
        }

        拷贝到apache的/cgi-bin/目录之下。

三、C代码写的,编译后形成的二进制代码形式的CGI:

        示例一:hello.c,编译后成为 hello.exe,拷贝到apache的/cgi-bin/目录之下,该后缀为.cgi。

        #include <stdio.h>
        int main()
        {
         printf("Content-type:text/html/n/n");
         printf("<html>/n");
         printf("<head><title>welcome to c cgi.</title></head>/n<body>/n");
         printf("You are welcom!<br/>/n");
         printf("</body>/n</html>");
        }

        示例二:mult.c,编译后成为 mult.exe,拷贝到apache的/cgi-bin/目录之下,该后缀为.cgi。

        int main(void)
        {
         char *data;
         long m,n;

         printf("Content-Type:text/html; charset=gb2312;/n/n");

         printf("<html>/n");
         printf("<head><title>乘法结果</title></head>/n<body>/n");
         printf("<h3>乘法结果</h3>/n");

         data = getenv("QUERY_STRING");

         if(data == NULL)
          printf("<P>错误!数据没有被输入或者数据传输有问题</p>/n");
         else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
          printf("<P>错误!输入数据非法。表单中输入的必须是整数数字。</p>/n");
         else
          printf("<P>%ld 和 %ld 的积是:%ld。</p>/n",m,n,m*n);

         printf("</body>/n</html>");
         return 0;
        }

        请求与上述cgi的页面:

        <html>
        <head><title>C CGI测试</title></head>
        <body>
        <FORM ACTION="/cgi-bin/mult.cgi">
        <P>请在下面填入乘数和被乘数,按下确定后可以看到结果。</P>
        <INPUT NAME="m" SIZE="5" />
        <INPUT NAME="n" SIZE="5" /><BR /> 
        <INPUT TYPE="SUBMIT" VALUE="确定" /> 
        </FORM>
        </body>
        </html>

        在此注意的是,用Turboc编译形成的16位DOS子程序,apache无法访问。出现服务器500错误,error.log日志记录会显示:

[Thu Aug 09 15:34:37 2007] [error] [client 127.0.0.1] (OS 5)拒绝访问。  : couldn't spawn child process: F:/php_code/cgi-bin/HELLO.cgi

        而用VC6.0编译形成的32位Windows Console Application则没有问题。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值