windows下,使用c写了一个简单的cgi程序,生成exe类型的可执行文件,代码如下:
#include<stdio.h>
int main()
{
printf("Content-Type:text/html\n\n");
printf("Hello,world!\n");
return ;
}
怎样使apache加载生成的exe文件?只需要两步即可实现:
1、配置cgi程序所在目录
2、使apache能该识别exe文件
对应到apache的配置文件分别为:
A:
<IfModule alias_module>代码块中增加:
ScriptAlias /cgi-bin/ "E:/c/"
红色字体为cgi程序(此处指生成的exe文件)所在的目录
注意:需要修改两处,在该代码块下方不远处也有一个地方需要修改,如下:
<Directory "E:/c">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
B:
<IfModule mime_module>代码块中修改或者增加如下两行:
AddType text/html .exe
AddHandler cgi-script .exe .cgi
假如生成的exe文件名为:test.exe
此时将test.exe拷贝到E:\C目录下,并重启apache。
浏览器中运行http://localhost/cgi-bin/test.exe,此时若看到Hello,world!则说明,配置成功。
扩展:
在c语言中接受get、post数据,范例如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *data;
long m,n; printf("Content-Type:text/html\n\n");
printf("<title>c语言生成html</title>"); data = getenv("QUERY_STRING");
if(NULL == data)
printf("<p>Error.</p>");
else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!= )
printf("<p>Please input number.</p>");
else
printf("<p>m:%ld;n:%ld.</p>",m,n);
return ;
}