Creating CGI Scripts

CGI scripts are computer programs. Chapter 4, "Mastering Shells and Shell Scripting," describes one type of programming language: shell scripts. You can use a shell script as a CGI script, although Perl is a more common CGI scripting language. I can't provide a complete guide to writing CGI scripts in this chapter, but I can provide a brief overview of the process. For more information, consult a book on Apache or your scripting language of choice. The Apache website also includes pages on CGI scripting, at http://httpd.apache.org/docs/howto/cgi.html and

http://httpd.apache.Org/docs-2.0/howto/cgi.html. Four key points about CGI scripts are:

• CGI scripts use standard input (stdin) and standard output (stdout) for interaction with the user.

• Prior to the bulk of the output, CGI scripts should generate a header that identifies the output's MIME type. For standard HTML, this line should read Content-type: text/html\r\n\r\n. (The \r\n codes stand for new lines—use two of them to separate the Content-type header from the bulk of the document.)

Typically, you should generate output that's in HTML format. Of course, you can create other formats, but as the Web is mostly based on HTML, that's the preferred format. If you create a CGI script to generate graphics or specialized file types, you must change the Content-type header appropriately.

• If your CGI script accepts input, the input appears as field/value pairs, where the field is the name of the field in a web form and the value is the value entered by the user. The field and value are separated from each other by an equal sign (=), and field/value pairs are separated from each other by ampersands (&). For instance, your script might receive back input such as fname=Elwyn&lname=White, telling you that the user entered Elwyn in the fname field and White in the Iname field.

0 0

Post a comment