如何在windowsXP下运行用c 编写cgi?
前提是安装apache服务器,apache服务器的conf里面在<IfModule mime_module>里面配置好。
AddType text/html .exe
AddHandler cgi-script .exe .cgi
然后只要把exe文件放到cgi-bin下面就好了
http://127.0.0.1/cgi-bin/1.exe
就可以访问了
cout/printf出来的会输出到网页上
添加效果如下图所示:
不过注意的是:第一行一定要输出一个空行。
示例如下:
//2012-12-4-------------------------------
//仅在VC6.0平台下测试可得好多环境变量都不存在!
//而在安装了Apache服务器后显示会有所不同。
int main ( int argc, char *argv[] ){ char *env_var[24] ={ "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE", "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING", "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", "HTTP_HOST", "HTTP_USER_AGENT", "PATH", "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT", "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME", "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN", "SERVER_NAME", "SERVER_PORT", "SERVER_PROTOCOL", "SERVER_SIGNATURE", "SERVER_SOFTWARE" }; printf ("%s\n","Content-Type: text/html\n" ); printf ( "%s\n","<?xmlversion = \"1.0\"?>" ); printf ("%s\n","<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML1.1//EN\" " ); printf ("%s\n","\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"); printf ( "%s\n","<htmlxmlns = \"http://www.w3.org/1999/xhtml\">" ); printf ("%s\n","<head><title>CGI环境变量</title></head><body>" ); printf ( "%s\n","<tableborder = \"0\" cellspacing = \"2\">" ); for( int i=0;i<24;i++ ){ printf ("%s%s%s\n","<tr><td>",env_var[i],"</td><td>"); char *value = getenv( env_var[i]); if ( value ) { printf ( "%s",value ); } else { printf ( "%s","CGI环境变量不存在." ); } printf ("%s\n","</td></tr>" ); } printf ("%s\n","</table></body></html>" ); printf ( "%s\n","C语言编写" ); return EXIT_SUCCESS;}
输出结果显示:
注意:以上结果是在安装了Apache服务器后的结果,如下图:
示例及显示结果:
void main(){ printf("Contenttype:text/html\n\n"); printf("<html>\n"); printf("<head><title>anHTML Page From a CGI</title></h ead>\n"); printf("<body><br>\n"); printf("<h2>This is an HTML page generated from with in a CGI program...</h2>\n"); printf("<hr><p>\n"); printf("<ahref=\"../output#two\"><b> Go back to output page</b></a>\n"); printf("</body>\n"); printf("</html>\n"); fflush(stdout);}