2024年1月22日发(作者:)

#include using namespace std;int main (){ cout << "Content-type:text/htmlrnrn"; cout << "n"; cout << "n"; cout << "Hello World - First CGI Programn"; cout << "n"; cout << "n"; cout << "

Hello World! This is my first CGI program

n"; cout << "n"; cout << "n";

return 0;}获取CGI的环境变量#include #include using namespace std;const string ENV[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"};

int main(){ cout<<"Content-type:text/htmlrnrn"; cout<<"n"; cout<<"n"; cout<<"CGI Envrionment Variablesn"; cout<<"n"; cout<<"n"; cout<<"

"; for(int i=0;i<24;i++){ cout<<"n"; } cout<<"
"<"; char *value = getenv(ENV[i].c_str());

if(value!= 0){ cout << value; }else{ cout<<"Environment variable does not exist."; } cout<<"

n"; cout<<"n";

return 0;}

GET参数的获取#include#include#include#includeusing namespace std;vector StringSplit(const string& sData, const string& sDelim){ vectorvItems; (); string::size_type bpos = 0; string::size_type epos = 0; string::size_type nlen = (); while ((epos=(sDelim, epos)) != string::npos){ _back((bpos, epos-bpos)); epos += nlen; bpos = epos; } _back((bpos, ()-bpos)); return vItems;}int main(){ cout << "Content-type:text/htmlrnrn"; cout << "n"; cout << "n"; cout << "Hello World - First CGI Programn"; cout << "n"; cout << "n";

cout<<"get parameter:
"; char *value = getenv("QUERY_STRING");

if(value!= 0){ /*"a=1&b=2&c=3"*/ vectorparas=StringSplit((const string)value,"&"); vector::iterator iter=(); for(;iter!=();iter++){ vectorsinglepara=StringSplit(*iter,"="); cout<"; } cout << "n"; cout << "n";

return 0;}