下面的 CGI 程序列出了所有的 CGI 变量。
实例
#include<iostream>#include<stdlib.h>#include<string>usingnamespacestd; conststringENV[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"}; intmain(){ cout << "Content-type:text/html\r\n\r\n"; cout << "\n"; cout << "\n"; cout << "CGI 环境变量\n"; cout << "\n"; cout << "\n"; cout << "\"0\" cellspacing = \"2\">"; for(inti = 0; i < 24; i++ ) { cout << "\n"; } cout << "
" << ENV[i] << " | "; // 尝试检索环境变量的值 char *value = getenv(ENV[i].c_str()); if(value != 0){ cout << value; }else{ cout << "环境变量不存在。"; } cout << " |
<\n"; cout << "\n"; cout << "\n"; return0;}