The Common Gateway Interface (CGI) is a standard
protocol for interfacing external application software
with an information server, commonly a web server.

The task of such an information server is to respond
to requests (in the case of web servers, requests from
client web browsers) by returning output. Each time a
request is received, the server analyzes what the
request asks for, and returns the appropriate output.
The two simplest ways, for the server, of doing this
are the following:

if the request identifies a file stored on disk,
return the contents of that file;
if the request identifies an executable command and
possibly arguments, run the command and return its
output
CGI defines a standard way of doing the second. It
defines how information about the server and the
request is passed to the command in the form of
arguments and environment variables, and how the
command can pass back extra information about the
output (such as the type) in the form of headers.

CGI is usually used with Python and Perl.  However,
there is nothing to stop you from using FORTRAN, C, or
even QBASIC.

Which brings us to...

THE QBASIC CGI TUTORIAL:

The first task is to write a very simple program which
just prints out an HTML document. This should include
all the usual <head> ... </head> and <title> ...
</title> commands. However, there are two things to
note.

First of all, the first two lines of the print out are
special. The first line must be

Content-type: text/html

so that the browser knows what kind of document it is
and therefore how to display it. In this case it is a
text document of the subclass HTML. You can also send
back plain ASCII documents by using text/plain
instead. Sound samples and images also have to have
the content type specified appropriately. The second
line must be blank (ie it must contain just a line
feed). This line is reserved for future development.

Let's try it:

PRINT "Content-type: text/html;charset=us-ascii"
PRINT
PRINT "<!DOCTYPE HTML PUBLIC " + CHR$(34) +
"-//W3C//DTD HTML 4.01//EN" + CHR$(34)
PRINT CHR$(34) +
"http://www.w3.org/TR/html4/strict.dtd" + CHR$(34) +
">" ' DOCTYPE declaration
PRINT "<html>"
PRINT "<head>"
PRINT "<title>Hello World</title>"
PRINT "</head>"
PRINT "<body>"
PRINT "<h1>Hello, World!</h1>"
PRINT "<p>"
PRINT "<a
href='http://validator.w3.org/check?uri=referer'> <img
"
PRINT "
src='http://www.w3.org/Icons/valid-html401'"
PRINT "    alt='Valid HTML 4.01 Strict' height='31'
width='88'>"
PRINT "</a>"
PRINT "</p>"
PRINT "</body>"
PRINT "</html>"
END

HTML allows us to use single quotes to quote
attributes, so we don't have to use CHR$(34).

To connect this to the internet, you use your web
server program.  Two web server programs are Apache
and Abyss.

http://en.wikipedia.org/wiki/Abyss_Web_Server

http://en.wikipedia.org/wiki/Apache_HTTP_Server

I was sucessfully able to get a CGI program that was
compiled with QB 7.1 running using Abyss.  You may
also be able to configure it so it runs the code using
QBASIC /RUN

To set up a CGI program in Abyss:

In the Scripting Parameters dialog, use the
Interpreters table to add the QBASIC interpreter and
the filename extensions it handles.  Alternately, add
the program to the list of scripts (screenshot:
http://img149.imageshack.us/img149/752/cgiwu5.png)

When a CGI script is called several environment
variables are set.

These variables include:

SERVER_SOFTWARE
SERVER_NAME
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.1
SERVER_PORT = 80
REQUEST_METHOD (either GET or PUT)
HTTP_ACCEPT
PATH_INFO
PATH_TRANSLATED
SCRIPT_NAME (name of the script)
QUERY_STRING = (string which you got using GET)
REMOTE_HOST =
REMOTE_ADDR (user's IP)
REMOTE_USER =
CONTENT_TYPE =
CONTENT_LENGTH =

You can access these using the ENVIRON$ function.

Usually the information supplied by the QUERY_STRING
variable should come from the user pressing buttons
and entering text in the HTML document. It is this
information we would like to package up and send to
the CGI script. Each group of buttons and text boxes
is called a form, and forms are enclosed between the
HTML tags <form> ... </form>. You also have to tell it
the URL to send the information to, and how the
information is sent. The result is something like
this:

A submit button is the input device that actually
calls the URL. It has a value which is the message
that appears on the button. Here is the code for a
form with just a submit button in it. When you click
on the submit button the URL specified in the form's
action is called.
<form action="whatever.exe"
     method="GET">
<input type="submit" value="Click me">
</form>

A checkbox is a simple on/off button. A checkbox has a
name (its key) and a value that this key has when the
box is checked. As an example, here is the HTML code
for a form with a checkbox and a submit button in it.
<form action="whatever.exe"
     method="GET">
<input type="checkbox" name="lights" value="on">
<input type="submit" value="Do it">
</form>

Now if the submit button is clicked when the box is
checked then the information lights=on is packaged
into QUERY_STRING. However if the box is not checked
then no information is packaged into QUERY_STRING and
it remains empty. You have to add the message to the
textbox as ordinary HTML text.

Radio buttons are just like checkboxes except they are
grouped together and only one button in the group may
be selected at a time. All the buttons in a group must
have the same name and each one should have a
different value. You can also specify which buttons
(if any) are checked initially. When the submit button
is clicked the name and the value of the selected
button are packaged up for QUERY_STRING.
Here is some example code for five such buttons. They
are all of type radio, and are in the group named
cert. The 15 button is checked initially.

<form action="whatever.exe"
     method="GET">
<input type="radio" name="cert" value="u">  U
<input type="radio" name="cert" value="pg"> PG
<input type="radio" name="cert" value="12"> 12
<input type="radio" name="cert" value="15"
checked="checked"> 15
<input type="radio" name="cert" value="18"> 18
<input type="submit" value="Certify">
</form>

Text input devices. These are simply boxes into which
the user can enter some text which is then packaged up
under a particular name. Here is some example code for
two text boxes and a submit button. The <br> tag
causes a line break.

<form action="whatever.exe"
     method="GET">
Director: <input type="text" name="dir">  <br>
Producer: <input type="text" name="prod">
<input type="submit" value="Fire">
</form>

You can use single quotes instead of double quotes if
you want it to work easier in QBASIC.

Example:

DECLARE FUNCTION FACTOR& (N&)
DEFLNG A-Z
PRINT "Content-type: text/html;charset=us-ascii"
PRINT
PRINT "<!DOCTYPE HTML PUBLIC " + CHR$(34) +
"-//W3C//DTD HTML 4.01//EN" + CHR$(34)
PRINT CHR$(34) +
"http://www.w3.org/TR/html4/strict.dtd" + CHR$(34) +
">"
PRINT "<html>"
PRINT "<head>"
PRINT "<title>Factor Tree</title>"
PRINT "</head>"
PRINT "<body>"
PRINT "<pre>"
QSTR$ = UCASE$(ENVIRON$("QUERY_STRING"))
N = VAL(MID$(QSTR$, INSTR(QSTR$, "NUM=") + 4))
DO
x = FACTOR(N)
IF x = -1 THEN PRINT TAB(43); N: EXIT DO
PRINT x; "<"; STRING$(39 - LEN(STR$(x)), "-"); "|"; N
PRINT SPACE$(41); "|"
N = N \ x
LOOP
PRINT "</pre>"
PRINT "<form action='factor.exe' method='GET'>"
PRINT "<p>"
PRINT "<input type='text' size='11' name='num' >"
PRINT "<input type='submit' value='Factor!' >"
PRINT "</p>"
PRINT "</form>"
PRINT "<p>"
PRINT "<a
href='http://validator.w3.org/check?uri=referer'> <img
"
PRINT "
src='http://www.w3.org/Icons/valid-html401'"
PRINT "    alt='Valid HTML 4.01 Strict' height='31'
width='88'>"
PRINT "</a>"
PRINT "</p>"
PRINT "</body>"
PRINT "</html>"
END

FUNCTION FACTOR (N)
FOR I = 2 TO SQR(N)
IF (N MOD I) = 0 THEN FACTOR = I: EXIT FUNCTION
NEXT
FACTOR = -1
END FUNCTION


Sometimes, getting lost is the most straightforward path to finding ourselves.