Get CGI environment variables

1.CGIIntroduction to environment variables

CGIProgram inheritance System environment variables,CGI environment variables, inCGIThe program is initialized at startup and destroyed at the end. When aCGIThe program is not usedHTTPWhen the server is called, its environment variables are basically system environment variables. When it belongs to the HTTP server call, his environment variables will be more than the following about< /span>HTTPserver, client,CGItransmission process and other items.

CGIThere are three types of environment variables related to the request. Variables, environment variables related to the server, and environment variables related to the client.

2.The following are some environmental variables

Environmental Variables
meaning

SERVER_NAME
CGIhost name and IP address when the script is running.

SERVER_SOFTWARE
The type of your server such as: CERN/ 3.0 orNCSA/1.3.

GATEWAY_INTERFACE
< span style="font-family:宋体">RunCGIversion. span>ForUNIXserver< /span>, This is CGI/1.1.

SERVER_PROTOCOL
server runningHTTPprotocol. here When it isHTTP/1.0.

SERVER_PORT
server RunningTCP mouth, usuallyWebThe server is80.

REQUEST_METHOD
POST orGET, depending on how your form was submitted.

HTTP_ACCEPT
Browser can directly receiveContent-types, YesHTTP Accept headerDefinition.

HTTP_USER_AGENT
Submit The name and version of the browser of the form and its Additional information on his platform.

HTTP_REFERER
Submit the text of the form URL, Not all browsers send this information, don’t rely on it

PATH_INFO
Additional path information, < span style="font-family:宋体">Sent by the browser through the GET method .

PATH_TRANSLATED
inPATH_INFO The path information specified by the system.

SCRIPT_NAME
point to thisCGI script path , is inURL “>shown in(such as, /cgi-bin/thescript).

QUERY_STRING
Script parameters or form input items(if usedGET span>Submit). QUERY_STRING Include URLthe parameters after the question mark.

REMOTE_HOST
The host name of the submitted script, this value cannot be set.< /p>

REMOTE_ADDR
the host that submitted the scriptIPaddress.

REMOTE_USER
The username of the user who submitted the script. If the server’sauthentication is activated, this The value can be set.

REMOTE_IDENT
IfWebthe server is on span>ident (A protocol that confirms that users connect to you)Run, The system for submitting the form is also runningident, this variable It containsidentreturn value.

CONTENT_TYPE
If the form is usedPOST “>Submit, this value will beapplication/x-www-form-urlencoded. in the upload file form, content-type Yesmultipart/form-data.

CONTENT_LENGTH
For forms submitted with POST, The number of bytes of the standard input port.

3.GetCGIvariables

#include #include #include const std::string ENV[]={ "SERVER_NAME", "SERVER_SOFTWARE", "GATEWAY_INTERFACE", "SERVER_PROTOCOL", "SERVER_PORT", "REQUEST_METHOD", "HTTP_ACCEPT", "HTTP_USER_AGENT ", "HTTP_REFERER", "PATH_INFO", "PATH_TRANSLATED", "SCRIPT_NAME", "QUERY_STRING", "REMOTE_HOST", "REMOTE_ADDR", "REMOTE_USER", "REMOTE_IDENT", "CONTENT_TYPE", "CONTENT_LENGTH"};int get ){ int i = 0; std ::cout<<"Content-type:text/html\r\n\r\n"; std::cout<<"\n"; std::cout<<"\n" ; std::cout<<" CGI Envrionment Variables\n"; std::cout<<"\n"; std::cout<<"\n" ; //std::cout<<"

Hello World! This is my first CGI program

\n"; std::cout<<""; for(i=0;i< sizeof(ENV)/sizeof(ENV[0]);i++) {std::cout<<""; std::cout<<"< td>" <"; std::cout<<""; std::cout<< "\n";} std::cout<<"
"; char *value = getenv(ENV[i].c_str()); if(NULL! = value) {std::cout<< value;} else {std::cout<<"Environment variable does not exist.";} std::cout<<"
"; std::cout<<"\n"; std::cout<<"\ n"; //std::cout<<"\n"; return 0;}int main(){ get_cgi_env(); return 0;}

g++ 2.cpp -o get_cgi_env -g


copy the executable file to the cgi-bin directory of the http server

root@ubuntu:/share/http/apache2.2.32-install# cp /share/get_cgi_env cgi-bin/


Browser input

http://192.168.1.211:8080/cgi-bin/get_cgi_env

< p>

Browser output

< tr>

SERVER_NAME 192.168.1.211
SERVER_SOFTWARE Apache/2.2.32 (Unix)
GATEWAY_INTERFACE CGI/1.1
SERVER_PROTOCOL HTTP/1.1
SERVER_PORT 8080
REQUEST_METHOD GET
HTTP_ACCEPT text/html,application/xhtml+xml,applic ation/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.1) AppleWebKit /537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0
HTTP_REFERER Environment variable does not exist.
PATH_INFO Environment variable does not exist.
PATH_TRANSLATED Environment variable does not exist.
SCRIPT_NAME /cgi-bin/get_cgi_env
QUERY_STRING< /td>

REMOTE_HOST Environment variable does not exist.
REMOTE_ADDR< /td>

192.168.1.100
REMOTE_USER Environment variable does not exist.
REMOTE_IDENT Environment variable does not exist.
CONTENT_TYPE Environment variable does not exist.
CONTENT_LENGTH Environment variable does not exist.

< /p>

Leave a Comment

Your email address will not be published.