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 ... and ... 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 declaration PRINT "" PRINT "" PRINT "Hello World" PRINT "" PRINT "" PRINT "

Hello, World!


" PRINT " Valid HTML 4.01 Strict" PRINT "" PRINT "

" PRINT "" PRINT "" 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. 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: 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
. 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.
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.
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.
U PG 12 15 18
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
tag causes a line break.
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 "" PRINT "" PRINT "" PRINT "Factor Tree" PRINT "" PRINT "" PRINT "
N = VAL(MID$(QSTR$, INSTR(QSTR$, "NUM=") + 4))
PRINT x; "<"; STRING$(39 - LEN(STR$(x)), "-"); "|"; N
PRINT SPACE$(41); "|"
N = N \ x



" PRINT " Valid HTML 4.01 Strict" PRINT "" PRINT "

" PRINT "" PRINT "" 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.