- What is CGI?
CGI stands for Common Gateway Interface and is a means by which a WWW browser client can interact with software running on a WWW server. The user sends requests to the server using the browser, the server launches the CGI application specified by the user, and passes the user's parameters to it. The CGI application generates a HTML page which is returned to the user's browser by the server when the CGI application terminates.
- HTML Forms
The most common way of collecting information from a user for passing to a CGI application as parameters is with an HTML form. Information is entered into the HTML form by the user & one of the form parameters (coded into the HTML) is the CGI application to be launched when the user presses the submit button.
- Environment Variables
Windows HTTPD communicates with the CGI application using environment variables. When the CGI application executes, it looks for various environment variables and reads information from them including the name of the file that contains the information collected from the user and the name of the file to which all the HTML code to be returned to the user should be written.
- Summary of the CGI process
- The user fills in HTML form & presses the submit button
- The browser sends the information filled into the form and the form parameters (including the CGI application to be launched) to the server.
- The server takes the form information and
- writes it to a local file
- sets an environment variable to the name of that file
- sets another environment variable to the name of the output file
- launches the CGI application
- When the CGI application runs it:
- reads the names of the user input & output files from the environment variables
- generates the output based on the input
- writes the output to the output file
- terminates
- The server returns the contents of the output file to the browser for the user to read
- The CGI Application
Based on the process above, all our Turbo Pascal CGI application has to do is
- Read the input data file name from an environment variable. The environment variable concerned is CONTENT_FILE. The GetEnv function can be used for this purpose.
- Read the output file name from an environment variable. OUTPUT_FILE in this case. Again, GetEnv can be used.
- read the input data from the file specified by CONTENT_FILE. This is in the form
field1=field1data&field2=field2data&field3=field3data. . . etc
Any spaces entered by the user will be replaced by + signs. IE My Name becomes My+Name.
- process the data
- write the output to the output file as HTML code. The first line of the file must contain a content type specification in the form:
Content-Type: text/html
followed by a blank line
- terminate
- An example: Hello {users name}
First the application (HELLO.PAS):
program hello;
uses dos;
var
infile,outfile:text;
name,inputline:string[80];
equals,character:integer;
begin
assign (infile,getenv('CONTENT_FILE'));
assign (outfile,getenv('OUTPUT_FILE'));
{Open the content file, read the user's input & close the file}
reset(infile);
readln(infile,inputline);
close(infile);
{Extract the users name from the input line}
equals:=pos('=',inputline); {Find the position of the first = sign}
name:=copy(inputline,equals+1,length(inputline)-equals);
{Change + signs in name to spaces}
for character:=1 to length(name) do
if name[character]='+'
then name[character]:=' ';
{Open the output file, write the content type, write the
output & close the file}
rewrite(outfile);
writeln(outfile,'Content-Type: text/html');
writeln(outfile);
writeln(outfile,'<H1>Hello ',name,'</H1>');
writeln(outfile,'<A HREF="/hello.htm">Back</A><P>');
close(outfile);
end.
Then the form (HELLO.HTM):
<FORM METHOD="POST" ACTION="/cgi-bin/hello.exe">
Enter Your Name: <input name="name" size=20><P>
<input type=submit><P>
</FORM>
These two files are in the directory C:\TPCGI
- Installing the files
HELLO.PAS will of course have to be compiled. The resulting executable needs to be copied to C:\HTTPD\CGI-DOS\HELLO.EXE. Note that although the form refers to CGI-BIN, the executable is stored in the CGI-DOS directory.
HELLO.HTM is copied to the normal HTML directory: C:\HTTPD\HTDOCS\HELLO.HTM
Once these files are installed, you can retrieve the form file with the web browser by pointing it to http://10.0.6.10/hello.htm Remember to substitute the address of your machine for 10.0.6.10. You can then test the CGI application by filling in the form & pressing "Submit".
- Query Strings
Suppose there was a CGI application that took a surname from the user via a form and returned a list of people with that surname. If more information was available about each person listed, it would be nice to be able to just click on their name to retrieve it. A function like this is achieved by embedding a link in the output page like so:
<A HREF="\cgi-bin\details.exe?jsmith">John Smith</A>
This link would launch the CGI application DETAILS.EXE, but instead of the parameter jsmith being passed via the file pointed to by CONTENT_FILE, jsmith is stored directly in the environment variable QUERY_STRING. To retrieve this parameter from within DETAIL.EXE we would use a pascal statement like this:
name:=getenv('QUERY_STRING');
- What next?
You now have all the tools you need to write CGI programmes to interact with users of your Windows HTTPD web site. Now you have to come up with the applications.
An application that comes quickly to mind is conducting surveys. You could set up a form with fields for Age, Sex, and Favorite Food, or perhaps Date, Temperature and Rainfall. When the user submits the form, the CGI application could write the three fields to a data file, with commas between each field, and send a page back to the user thanking them for participating in the survey. When the survey period finishes, the data file can be read into a spreadsheet or database for analysis.
A system like this could be used on an Intranet server to collect data from multiple students, over a period, into a single database.
A more sophisticated version for older students could ask the person filling the form to enter an email address if they would like to get a copy of the survey results emailed to them.