Main Page

Previous Page
Next Page

[Page 928 (continued)]

19.10. Sending Input to a CGI Script

Though preset environment variables provide much information, we would like to be able to supply any type of data to our CGI scripts, such as a user's name or a search-engine query. The environment variable QUERY_STRING provides a mechanism to do just that. The QUERY_STRING variable contains information that is appended to a URL in a get request. For example, the URL


causes the Web browser to request a CGI script (cgi-bin/script.cgi) with query string (state=California) from www.somesite.com. The Web server stores the query string following the ? in the QUERY_STRING environment variable. The query string provides parameters that customize the request for a particular client. Note that the question mark (?) is not part of the resource requested, nor is it part of the query string. It serves as a delimiter (or separator) between the two.

Figure 19.9 shows a simple example of a CGI script that reads data passed through the QUERY_STRING. The data in the query string can be formatted in a variety of ways. The CGI script reading the query string must know how to interpret the formatted data. In the example in Fig. 19.9, the query string contains a series of name-value pairs delimited by ampersands (&), as in name=Jill&age=22.

Figure 19.9. Reading input from QUERY_STRING.
(This item is displayed on pages 929 - 930 in the print version)

 1  // Fig. 19.9: querystring.cpp
 2  // Demonstrating QUERY_STRING.
 3  #include <iostream>
 4  using std::cout;
 6  #include <string>
 7  using std::string;
 9  #include <cstdlib>
10  using::getenv;
12  int main()
13  {
14     string query = "";
16     if ( getenv( "QUERY_STRING" ) ) // QUERY_STRING variable exists    
17        query = getenv( "QUERY_STRING" ); // retrieve QUERY_STRING value
19     cout << "Content-Type: text/html\n\n"; // output HTTP header
21     // output XML declaration and DOCTYPE
22     cout << "<?xml version = \"1.0\"?>"
23        << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" "
24        << "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
26     // output html element and some of its contents
27     cout << "<html xmlns = \"http://www.w3.org/1999/xhtml\">"
28        << "<head><title>Name/Value Pairs</title></head><body>";
29     cout << "<h2>Name/Value Pairs</h2>";
31     // if query contained no data
32     if ( query == "" )
33        cout << "Please add some name-value pairs to the URL above.<br/> Or"
34           << " try <a href=\"querystring.cgi?name=Joe&age=29\">this</a>.";
35     else // user entered query string
36        cout << "<p>The query string is: " << query << "</p>";
38     cout << "</body></html>";
39     return 0;
40  } // end main

In line 16 of Figure 19.9, we pass "QUERY_STRING" to function getenv, which returns the query string or a null pointer if the server has not set a QUERY_STRING environment variable. [Note: The Apache HTTP Server sets QUERY_STRING even if a request does not contain a query stringin this case, the variable contains an empty string. However, some servers, such as Microsoft's IIS, set this variable only if a query string actually exists.] If the QUERY_STRING environment variable exists (i.e., getenv does not return a null pointer), line 17 invokes getenv again, this time assigning the returned query string to string variable query. After outputting a header, some XHTML start tags and the title (lines 1929), we test if query contains data (line 32). If not, we output a message instructing the user to add a query string to the URL. We also provide a link to a URL that includes a sample query string. Query string data may be specified as part of a hyperlink in a Web page when encoded in this manner. The contents of the query string are output by line 36.

This example simply demonstrated how to access data passed to a CGI script in the query string. Later chapter examples show how to break a query string into useful pieces of information that can be manipulated using separate variables.

Previous Page
Next Page