> Software > cgihtml

Next Previous Contents

6. Example programs

6.1 test.cgi

test.cgi is a simple test program. It will display the CGI environment, and if there is any input, it will parse and display those values as well.

6.2 query-results

This is a generic forms parser which is useful for testing purposes. It will parse both GET and POST forms successfully. Simply call it as the form "action", and it will return all of the names and associated values entered by the user.

query-results also works from the command line. For instance, if you run query-results from the command line, you will see:

Content-type: text/html

<html> <head>
<title>Query Results</title>


--- cgihtml Interactive Mode ---
Enter CGI input string.  Remember to encode appropriate characters.
Press ENTER when done:

Suppose you enter the input string:


Then query-results will return:

 Input string: name=eugene&age=21
String length: 18
--- end cgihtml Interactive Mode ---

<h1>Query results</h1>
  <dt> <b>name</b>
  <dd> eugene
  <dt> <b>age</b>
  <dd> 21
</body> </html>

This feature is extremely useful if you are debugging code. query-results will also handle file upload properly and transparently. It will save the file to the directory defined by UPLOADDIR (/tmp by default).

6.3 mail.cgi

This is a generic comments program which will parse the form, check to see if the intended recipient is a valid recipient, and send the e-mail if so.

You will want to edit two things in the source file: WEBADMIN and AUTH. WEBADMIN is the complete e-mail address to which the comments should be sent by default. AUTH is the exact location of the authorization file.

The authorization file is simple a text file with a list of valid e-mail recipients. Users will only be able to use this program to send e-mail to those listed in the authorization file. Your file might look like this:

In the above case, you would only be able to send e-mail to and Make sure you include the value of WEBADMIN in your authorization file.

The following are valid variables in your form:

If there is no to variable defined in the form, the mail will be sent to WEBADMIN by default. mail.cgi will reject empty forms.

mail.cgi adds a "X-Sender:" header on each message so recipients know that the message was sent by this program and not by a regular mail client.

6.4 index-sample.cgi

Imagemaps have become increasingly popular to use on home pages. Unfortunately, imagemaps are not lynx friendly; if you forget to include some sort of text index as well, lynx users will not be able to access any of your subpages.

You can circumvent this problem by using a CGI program as your home page rather than an HTML page (or by using server-side includes). This CGI program will determine whether your browser is a graphics or text-browser. If it is a text-browser, it will send a text HTML file, otherwise it will send a graphics HTML file.

You will need to create two HTML files: a graphical and a text one. Place the names of these files in the macros: TEXT_PAGE and IMAGE_PAGE.

6.5 ignore.cgi

Sends a status code of 204, signifying no content. If you use imagemaps, you can set "default" to /cgi-bin/ignore.cgi. Whenever someone clicks on a part of the picture which is undefined, the server will just ignore the request.

Next Previous Contents