如何在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.html#two\"><b> Go back to output.html page</b></a>\n");
printf("</body>\n");
printf("</html>\n");
fflush(stdout);