1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
<HTML><HEAD>
<TITLE>CGI Interface -- Output from CGI Programs</TITLE>
<LINK rel=Previous href="cgi-ch4.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="cgi-ch6.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="cgi-ch4.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="cgi-ch6.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="8693">
</a><h3>Output from CGI Programs</h3>
<p><a name="8735">
</a>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:<Table Border = "3">
<tr><td><p><a name="15874">
</a>C or C++</p>
<td><pre> <a name="15876"></a><code>#include <stdio.h>
</code> <a name="16017"></a>#include <stdlib.h>
<a name="16183"></a>printf(<code>"</code><HEAD><TITLE>Hello</TITLE></HEAD><code>"</code>);
<a name="16184"></a>printf(<code>"</code><BODY>You are using %s.</BODY><code>"</code>,
getenv(<code>"</code>HTTP_USER_AGENT<code>"</code>) );
<a name="20142"></a>
</pre><p>
<tr><td><p><a name="15883">
</a>Perl</p>
<td><pre> <a name="16224"></a><code>print "</code><HEAD><TITLE>Hello</TITLE></HEAD><code>"</code>;
<a name="16225"></a>print <code>"</code><BODY><code>"</code>;
<a name="15885"></a>print <code>"</code>You are using $http_user_agent.</BODY><code>";
</code> <a name="20159"></a>
</pre><p>
<tr><td><p><a name="15887">
</a>Bourne shell</p>
<td><pre> <a name="16080"></a><code>echo \</code><HEAD\>\<TITLE\>Hello\</TITLE\>\</HEAD\>
<a name="16092"></a>echo \<BODY\>
<a name="16190"></a>echo You are using $HTTP_USER_AGENT.\</BODY\>
<a name="20183"></a>
</pre><p>
</Table></p>
<a name="16454">
</a><h4>HTTP Headers</h4>
<p><a name="16985">
</a>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. </p>
<p><a name="17050">
</a>The AOLserver automatically generates some HTTP header information and your program can add other information to the header.</p>
<a name="16483">
</a><h4>Header Information Generated by AOLserver</h4>
<p><a name="16972">
</a>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:</p>
<pre> <a name="16586"></a>
<a name="16663"></a>HTTP/1.0 200 OK
<a name="16664"></a>MIME-Version: 1.0
<a name="16875"></a>Server: AOLserver/3.0
<a name="16665"></a>Date: Monday, 06-Nov-95 17:50:15 GMT
<a name="21549"></a>Content-length: 20134
</pre><p><p><a name="16896">
</a>However, if the name of your CGI program begins with "<code>nph-</code>", 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.</p>
<a name="17023">
</a><h4>Header Information Generated by Your Program</h4>
<p><a name="23200">
</a>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. <i>Your program should always send the Content-type header</i> (unless you are using the Location header). The other headers listed below it are optional. For example, </p>
<pre> <a name="17076"></a>
<a name="17078"></a>Content-type: text/html
<a name="17089"></a>
<a name="17090"></a><HTML>
<a name="17092"></a><HEAD><TITLE>My title</TITLE></HEAD>
<a name="17091"></a><BODY>text goes here...</BODY>
<a name="17093"></a></HTML>
</pre><p><a name="16802">
</a><h4>Content-type:</h4>
<p><a name="16793">
</a>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 <code>text/html</code>. If you are sending untagged text, send a Content-type of <code>text/plain</code>. If you send images, you might use a Content-type of <code>image/gif</code> or <code>image/jpeg</code>. You can send any type of output from your CGI program -- just be sure to specify the correct MIME type.</p>
<p><a name="16815">
</a>Example: <code>Content-type: text/html</code></p>
<a name="16438">
</a><h4>Content-encoding:</h4>
<p><a name="17151">
</a>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 <code>x-gzip</code> for GNU zip compression and <code>x-compress</code> for standard UNIX compression.</p>
<p><a name="17168">
</a>Example: <code>Content-encoding: x-compress</code></p>
<a name="16418">
</a><h4>Expires:</h4>
<p><a name="17173">
</a>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.</p>
<p><a name="17210">
</a>Example: <code>Expires: Monday, 06-Nov-95 17:50:15 GMT</code></p>
<a name="16420">
</a><h4>Location:</h4>
<p><a name="17225">
</a>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, <code>http://www.mysite.com/out/response.htm</code>), relative references in that file will be resolved using the information in the URL you specify. If you specify a relative URL (such as <code>/out/response.htm</code>), references in that file will be resolved using the directory that contains the CGI program.</p>
<p><a name="17302">
</a>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.</p>
<p><a name="17287">
</a>Example: <code>Location: http://www.my.org/outbox/accepted.html</code></p>
<a name="16421">
</a><h4>Status:</h4>
<p><a name="17314">
</a>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. </p>
<p><a name="18088">
</a>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.</p>
<p><a name="17318">
</a>Example: <code>Status: 401 Unauthorized</code></p>
<a name="16924">
</a><h4>Sending HTML</h4>
<p><a name="18139">
</a>To send a Web page to a reader's browser from a CGI program, first output this line <i>followed by a blank line</i>:</p>
<pre> <a name="18140"></a>
<a name="18141"></a>Content-type: text/html
</pre><p><p><a name="18142">
</a>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.</p>
<p><a name="18157">
</a>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.</p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="cgi-ch4.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="cgi-ch6.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|