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