The basic idea is that the CGI script builds a web page dynamically, by outputting HTML tags to stdout. The CGI script can be written in any language. I'll be writing it here simply in UNIX Shell.
The input comes in as the environment variable QUERY_STRING. If there is a single argument, QUERY_STRING will be of the form: fieldname=actualargument, so we need to edit it to remove the fieldname= bit at the front. As you will see in the HTML Form shortly, the fieldname here will be called "q":
#!/bin/sh echo "Content-type: text/html" echo echo '<html> <head> <title> CGI script </title> </head> <body>' argument=`echo "$QUERY_STRING" | sed "s|q=||"` echo " QUERY_STRING is: <b> $QUERY_STRING </b> <br>" echo "Actual argument is: <b> $argument </b> <br>"
<FORM METHOD="GET"
ACTION="http://computing.dcu.ie/cgi-bin/humphrys/demo/min-cgi-script">
<b> Enter argument: </b>
<INPUT size=40 name=q VALUE="">
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
The user sees:
where our C++ program writes HTML tags to stdout.prog "$argument"
where the C++ program just writes its usual output to stdout.echo "<pre>" prog "$argument" echo "</pre>"
Remember that anyone may send any input whatsoever to your CGI script, including attempts to run commands on your system. Even echoing in a shell script (as above) may not be safe, since echo recognises some switches and special characters.
The safest thing to do (and what I do in fact on this server, though I don't show it to you) is to switch into a HLL to process the input environment character by character before proceeding with safe and checked input.