A. Server-side include and CGI
With the Apache web server software, these special commands are activated only if they are in a file whose name has .shtml at the end (i.e., as an extension). Otherwise Apache doesn't know to look at the HTML; it just passes it along and doesn't notice these.
little_bit.html
into an HTML file myfile.shtml
, in this second file put a line
<!--#include file="little_bit.html"-->
at the place you want.
Sample use: If you have lots of HTML pages
where you want your name and email address at the end, you could
put those in a file contact_info.html
and then include that
into the other files. You'd have to name the other files with
.shtml
.
little.cgi
and include the
output as just one part of an HTML file myfile.shtml
, in this
file put a line
<!--#exec cgi="little.cgi"-->
The program little.cgi
should start its output with
print "Content-type: text/html\n\n";
(Note the extra blank line as usual). However, your CGI program
doesn't need to output title information and tags, so don't use
CGI_Header
.
Sample use: If you want to put an access counter on your home page, do this:
Step 1. If your home page is now index.html
rename it
index.shtml
Step 2. In the HTML for your home page, put a line
<!--#exec cgi="count.cgi"-->
where you want the count to appear.
Step 3. Write the counter script count.cgi
to output
only the
Content-type:
line, a blank line, and HTML giving the
count, for example
print "Content-type: text/html\n\n";
<HR> There have been 51 visitors to this page. <HR>
Remember <HR>
is for Horizontal Rule, which seems nice for
this message, or else you could omit <HR>
and end the line
with <BR>
instead. Your script will have a print statement
such as
print "<HR>There have been $count visitors to this page.<HR>\n";
OR you can put everything that doesn't change in your home page,
so that Step 2 has
<HR>
There have been <!--#exec cgi="count.cgi"--> visitors to this page.
<HR>
and Step 3 (your CGI script) has simply
print "Content-type: text/html\n\n";
print "$count\n";
B. Server redirect
Usually a CGI script produces a Content-type: ...
line followed
by a blank line and HTML. What the HTML says probably depends partly
on information computed in the script.
What if you want your CGI script always to produce the same HTML, for example HTML that simply has a thank-you message that is the same every time?
Here is how to do that:
Step 1. Make a file thanks.html
that has your message.
Step 2. Write your CGI script to output only this:
Location: thanks.html
followed by a blank line.
In other words, in your script use
print "Location: thanks.html\n\n";
That's all. This is called ``server redirect'' because the server
starts out thinking it might get an HTML page but when it
sees the Location:
information instead of Content-type:
it
just goes to that page.