[ Previous ] [ Contents ] [ Index ] [ Next ]

Output from CGI Programs

To send output from a CGI program to the reader's browser, you send the output to the standard output location. Different languages allow you to send text to standard output in different ways. Here are some examples:
C or C++
    #include <stdio.h>
    #include <stdlib.h>
    printf("<HEAD><TITLE>Hello</TITLE></HEAD>");
    printf("<BODY>You are using %s.</BODY>", 
getenv("HTTP_USER_AGENT") );
    

Perl
    print "<HEAD><TITLE>Hello</TITLE></HEAD>";
    print "<BODY>";
    print "You are using $http_user_agent.</BODY>";
    

Bourne shell
    echo \<HEAD\>\<TITLE\>Hello\</TITLE\>\</HEAD\>
    echo \<BODY\>
    echo You are using $HTTP_USER_AGENT.\</BODY\>
    

HTTP Headers

Messages sent between a Web browser and a Web server contain header information that the software uses to determine how to display or interpret the information. The header information is not displayed by the browser.

The AOLserver automatically generates some HTTP header information and your program can add other information to the header.

Header Information Generated by AOLserver

When your CGI program sends output to the standard output location, the server automatically adds the following HTTP header information before sending the output to the reader's browser:

    
    HTTP/1.0 200 OK
    MIME-Version: 1.0
    Server: AOLserver/2.1
    Date: Monday, 06-Nov-95 17:50:15 GMT
    Content-length: 20134

However, if the name of your CGI program begins with "nph-", the AOLserver will not parse the output you send. Instead, the output is sent directly to the client. In this case, you must include the information above in your output. Generally, it is best to avoid using this "non-parsed header" feature because any errors may be sent to standard output and could make the header information incorrect. Also, with non-parsed headers, the server does not interpret the output, so the response code and content length are written out as 0 (zero) and 0 (zero) in the access log file.

Header Information Generated by Your Program

You can specify header information at the beginning of the output you send back to the client. After the header, add a blank line and then start the output you want the reader to see. The blank line is required. Your program should always send the Content-type header (unless you are using the Location header). The other headers listed below it are optional. For example,

    
    Content-type: text/html
    
    <HTML>
    <HEAD><TITLE>My title</TITLE></HEAD>
    <BODY>text goes here...</BODY>
    </HTML>

Content-type:
You should always use this header to specify the MIME type of the output you are sending (unless you are using the Location header). If you are sending an HTML page as output, use a Content-type of text/html. If you are sending untagged text, send a Content-type of text/plain. If you send images, you might use a Content-type of image/gif or image/jpeg. You can send any type of output from your CGI program -- just be sure to specify the correct MIME type. Example: Content-type: text/html
Content-encoding:
Use this header if the output you are sending is compressed. The Content-type should specify the type of the uncompressed file. For example, use x-gzip for GNU zip compression and x-compress for standard UNIX compression. Example: Content-encoding: x-compress
Expires:
Use this header to specify when the browser should consider the file "out-of-date". Browsers can use this date to determine whether to load the page from their local cache of pages or to reload the file from the server. Example: Expires: Monday, 06-Nov-95 17:50:15 GMT
Location:
Use this header if you want to send an existing document as output. The server automatically sends the document you specify to the browser. You will probably want to specify a full URL for the Location. If you specify a complete URL (such as, http://www.mysite.com/out/response.htm), relative references in that file will be resolved using the information in the URL you specify. If you specify a relative URL (such as /out/response.htm), references in that file will be resolved using the directory that contains the CGI program. If you send a Location header, you do not need to send a Content-type header. However, you may want to send HTML-tagged text including a link to the location for browsers that do not support this type of redirection. You can specfy any type of URL as the output location. For example, you can send an FTP, Gopher, or News URL. Example: Location: http://www.my.org/outbox/accepted.html
Status:
The AOLserver sends a status code to the browser in the first line of every HTTP header. The default status code for success is "200 OK". You can send other status codes by specifying the Status header. Some browsers may not know how to handle all HTTP status codes, so your program should also send HTML output after the header to describe error situations that occur. Example: Status: 401 Unauthorized

Sending HTML

To send a Web page to a reader's browser from a CGI program, first output this line followed by a blank line:

    
    Content-type: text/html

Then, generate and output the HTML tags and content that make up the page. You can send any HTML tags you would normally use when creating pages.

If you use AOLpress to author pages, you can use Tools Show HTML to display the HTML tags and File Save As to save the tags to a separate file that you edit into your CGI program.

If the file you want to send already exists, you can use the Location header described in the previous section to send that file as output from the CGI program.

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1996 America Online, Inc.