CGI Environment Variables
CGI protocol is defined with a collection of environment variables that hold data set up by a browser. It defines methods such as GET and POST for sending data, and how much data will be sent to server. Other environment variables contain such information as visitor's IP address, visitor's browser type, etc. All the environment variables are stored in the special hash array or associate array called %ENV which is set each time when a script is called.
A list of environment variable defined by CGI standard is as shown below:
If the server supports user authentication, and the script is protects, this is the protocol-specific authentication method used to validate the user.
It specifies the media type of the data for queries, which have attached information, such as HTTP POST and PUT, this is the content type of the data.
The length (number of bytes) of information passed to the script.
The name and version of the protocol being used by the server to communicate with the script. Format: CGI/revision
It provides any extra path information, as given in the URL, for accessing this script. The extra information is sent as PATH_INFO to be decoded by the server before it is passed to the CGI script.
It gives the absolute file system path for access the script. The server provides a translated version of PATH_INFO, which takes the path and does any virtual-to-physical mapping to it.
Any additional information passed to the script after the ? mark in the URL which referenced this script is called the query information. It should not be decoded in any fashion.
It contains a fully qualified domain name of the client computer. If the host name cannot be determined, it should set REMOTE_ADDR to hold the IP address of the host and leave this variable unset.
The IP address of the remote client computer making the request.
The client machine's username. Usage of this variable should be limited to logging only.
A virtual path to the script being executed, used for self-referencing URLs.
The name used to authenticate the user for accessing the script.
The name and version of the information server software answering the request (and running the gateway).
The server's hostname, DNS alias, or IP address as it would appear in self-referencing URLs.
The name and revision of the information protocol this request came in with. Format: protocol/revision
The port number to which the request was sent.
The method with which the request was made. For HTTP, this is "GET", "HEAD", "POST", etc.
Gives a comma-separated list of MIME types that the client can accept.
Provides the address of the page where the request originated.
Specifies the name of the client program used to make the request.
Errors Codes for Client and Server
When there is an error the corresponding error code will be sent to client and the message will then displayed for the client. The error codes are as shown below.
Client Errors Code
400 Bad Request
402 Payment Required
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
411 Length Required
412 Precondition Failed
413 Request Entity Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested range not valid
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Redirection failed
HTTP Status Codes
HTTP Working Group: http://www.w3.org/Protocols/HTTP
RFC 2616(HTTP 1.1):http://www.cis.ohio-state.edu/htbin/rfc/rfc2616.html
101 Switching Protocols
200 OK, the request was fulfilled
201 Created, following a POST command, indicates the URI by which the newly created document should be known
202 Accepted, the request has been accepted for processing, but the processing has not been completed
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content
299 Miscellaneous information
300 Multiple Choices, multiple documents available
301 Moved Permanently
302 Moved temporarily
303 See other document
304 Not modified since last retrieval
305 Use Proxy
306 Switch proxy
307 Document moved temporarily
The first line of most CGI programs must be an HTTP header, as listed below, to inform the Web browser what type of output is expected. When the Perl print statement is used to send the script file to the browser, typical web HTML tags and display are then followed.
Text Content Type: text/plain
HTML page Content Type: text/html
Gif graphics Content Type: image/gif
Error Message Status: 402
Example 1: You will prepare a Perl CGI script and place it in the cgi-bin directory of a desired server. In addition, you will need to prepare a simple HTML web page for linking to the Perl script for viewing portion information of your browser.
The Perl CGI script:
The HTML Web page for calling the Perl CGI script:
The Web page for client side user:
The output from running Perl CGI located on the server:
Example 2: This Perl CGI program, readend.pl, is placed in the cgi-bin directory. Another HTML page called readenv.html is also placed on the server.
The Perl CGI program:
The HTML page can be either placed in the client machine or at server:
The readenv.pl is executed by the server and the send the result back to the client:
The compete list of environment variables are listed below:
PROMPT == $p$g
SERVER_PROTOCOL == HTTP/1.1
COMSPEC == C:\COMMAND.COM
NWLOGIN == G
PATH_TRANSLATED == C:\HTTPD\HTDOCS
HTTP_USER_AGENT == Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)
HTTP_ACCEPT == image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*
GATEWAY_INTERFACE == CGI/1.1
SERVER_SOFTWARE == OmniHTTPd/2.07
TMP == C:\WINDOWS\TEMP
WINDIR == C:\WINDOWS
REMOTE_ADDR == 184.108.40.206
BLASTER == A220 I7 D1 T2
SCRIPT_NAME == /cgi-bin/readenv.pl
TEMP == C:\WINDOWS\TEMP
WINBOOTDIR == C:\WINDOWS
CMDLINE == WIN
SERVER_NAME == eet499
SNDSCAPE == C:\WINDOWS
CONTENT_LENGTH == 0
REQUEST_METHOD == GET
VXIPNPPATH == C:\VXIPNP
PATH == C:\httpd;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\PERL\BIN\;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\PROGRA~1\NETWOR~1\MCAFEE~1;S:\WIN32\LOTUS\CCMAIL\;C:\PROGRAM;FILES\MTS;C:\VXIPNP\WIN95\BIN;C:\PROGRAM;FILES\MTS;C:\VXIPNP\WIN95\BIN;C:\HTTPD\PHP
SERVER_PORT == 80