The server and the CGI script communicate in four major ways. Once the browser has sent a request to the server, the server can then send it on to the CGI script. The CGI script gets its input from the server as:
Environment variables
Query strings
Standard input
Extra path information
After the CGI program gets the input from the server, it parses and processes it and then formats it so that the server can relay the information back to the browser. The CGI script sends output through the gateway by
Generating new documents on the fly
The CGI program is passed a number of environment variables from the server. The environment variables are set when the server executes the gateway program and are set for all requests. The environment variables contain information about the server, the CGI program, the ports and protocols, path information, etc., and they are always represented in uppercase. User input is normally assigned to the QUERY_STRING environment variable. In the following example, this variable has no value, because the user was never asked for input; that is, the HTML document has no INPUT tags.
(The CGI Perl Script) 1 #!/bin/perl 2 print "Content type: text/plain\n\n"; 3 print "CGI/1.1 test script report:\n\n"; 4 # Print out all the environment variables 5 while(($key, $value)=each(%ENV)){ 6 print "$key = $value\n"; } |
Have you ever noticed, when performing a search in your browser, you end up with a line that looks like this?
http://www.gogle.com/search?hl=en&q=related%3Awww.lynda.com%2Fhexh.html&btnG=Search
The information after the ? is sent by the browser to the server and assigned to the QUERY_STRING environment variable. It is a URL encoded by the browser; i.e., any nonalphanumeric characters in the string are represented by +, &, or a hexadecimal number preceded by a %, such a %2. The program on the server side will be responsible for decoding this string to make it legible. For our discussion, that program is a Perl CGI script, although it could be a C, C++, Tcl, Shell script, etc.
In the next example, we have appended a ? and string of text to the URL of the same CGI script we executed in Example 16.9. This string is encoded by the browser and assigned to the QUERY_STRING environment variable, making the information available to the Perl script. The next step would be to remove the %20 (hexadecimal value of a space); i.e., to decode the encoded string. All of this will be explained in detail on page 545, but for now, when you see this type of URL, the string after the ? is sent to your program in the QUERY_STRING environment variable.
A question mark has been appended to the URL, followed by a string of text.
The following example is an HTML file that will allow the user to print out all the environment variables by clicking on a hyper-link. When the browser displays this document, the user can click on the link here, and the CGI script printenv.pl will then be executed by the server. In the HTML document, the string here and the URL http://localhost/cgi-bin/printenv.pl are enclosed in the <A href></A> tags. If the link is ignored by the user, the browser displays the rest of the document. The following example is the HTML source file that will be displayed in the browser. The browser's output is shown in Figure 16.9.
(The HTML file with a hotlink to a CGI script)
1 <html>
2 <head>
3 <title>TESTING ENV VARIABLES</title>
</head>
<body>
4 <p>
<h1> MAJOR TEST </h1>
<p> if you would like to see the environment variables<br>
being passed on by the server, click .
5 <a href="http://localhost/cgi-bin/printenv.pl">here</a>
<p>text continues here...
</body>
</html> Explanation
|