用C写的,输出CGI的环境变量.
- 路径:/var/www/cgi-bin/
- 编译:gcc -o cgi_env cgi_env.c -std=c99
- 在浏览器中输入http://localhost/cgi-bin/cgi_env 或者 http://ip/cgi-bin/cgi_env
- //cgi_env.c
- #include <stdlib.h>
- #include <stdio.h>
-
- 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","<?xml version = \"1.0\"?>" );
- printf ( "%s\n","<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" " );
- printf ( "%s\n","\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" );
- printf ( "%s\n","<html xmlns = \"http://www.w3.org/1999/xhtml\">" );
- printf ( "%s\n","<head><title>CGI环境变量</title></head><body>" );
- printf ( "%s\n","<table border = \"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;
- }