输出CGI的环境变量

用C写的,输出CGI的环境变量.
  1. 路径:/var/www/cgi-bin/
  2. 编译:gcc -o cgi_env cgi_env.-std=c99
  3. 在浏览器中输入http://localhost/cgi-bin/cgi_env 或者 http://ip/cgi-bin/cgi_env
  1. //cgi_env.c
  2. #include <stdlib.h>
  3. #include <stdio.h>

  4. int main ( int argc, char *argv[] )
  5. {
  6.     char *env_var[ 24 ] = {
  7.     "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
  8.     "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
  9.     "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
  10.     "HTTP_HOST", "HTTP_USER_AGENT", "PATH",
  11.     "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
  12.     "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
  13.     "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
  14.     "SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
  15.     "SERVER_SIGNATURE","SERVER_SOFTWARE" };

  16.     printf ( "%s\n","Content-Type: text/html\n" );
  17.     printf ( "%s\n","<?xml version = \"1.0\"?>" );
  18.     printf ( "%s\n","<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" " );
  19.     printf ( "%s\n","\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" );
  20.     printf ( "%s\n","<html xmlns = \"http://www.w3.org/1999/xhtml\">" );
  21.     printf ( "%s\n","<head><title>CGI环境变量</title></head><body>" );
  22.     printf ( "%s\n","<table border = \"0\" cellspacing = \"2\">" );

  23.     for ( int i=0;i<24;i++ ) {
  24.         printf ( "%s%s%s\n","<tr><td>",env_var[i],"</td><td>" );
  25.         char *value = getenv( env_var[i]);

  26.         if ( value ) {
  27.             printf ( "%s",value );
  28.         }
  29.         else {
  30.             printf ( "%s","CGI环境变量不存在." );
  31.         }
  32.         printf ( "%s\n","</td></tr>" );
  33.     }
  34.     printf ( "%s\n","</table></body></html>" );
  35.     printf ( "%s\n","C语言编写" );
  36.     return EXIT_SUCCESS;
  37. }

你可能感兴趣的:(cgi)