本文介绍一个最简单的CGI程序.
开发环境:vc++ 6.0
运行环境: apache2.2
创建者:seasun/csdn
开发步骤:
1,在VC6里面建立一个win32 console application项目,名字为"basic"
2,在main函数中复制如下内容:
// basic.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//create by seasun
int main(int argc, char* argv[])
{
printf("Contenttype:text/html/n/n"); //根据HTTP协议,这里一定要有个空行。
printf("<html>");
printf("<body>");
printf("Hello CGI!/n");
printf("Run On Apache2.2/n");
printf("Create In Visual C++ 6.0/n");
printf("</body>");
printf("</html>");
return 0;
}
3,编译一个release版本的,将生成的exe文件 "basic.exe"复制到 apache的cgi-bin目录中,也可以将exe后缀名去掉.
4,在浏览器地址栏输入http://localhost/cgi-bin/basic.exe (或不要后缀名)回车后,就可以看到输出的结果
注意:如果没有 printf("Contenttype:text/html/n/n"); 这句,在apache的log文件中会发现如下信息:
"malformed header from script. Bad header="
这是说CGI程序的输出不符合HTTP协议造成的.根据HTTP协议,这里一定要有个空行。. 本文完,seasun