| Gsharp Training Material | Home | Table of Contents |
The server is normally set up so that when a page is requested from the cgi-bin directory then rather then sending the contents of the requested file, it runs the file and sends back the output instead.
It is also possible to specify that the server should run any files that ends in .cgi.
In other words, files in the cgi-bin directory must be executable and create an HTML file on stdout. The script does not care about the contents of the file - it just executes it and uses the output. The script can use the name/value pairs that it receives to determine the HTML that it creates.
Common languages for writing CGI scripts in are shell scripts, perl scripts and the Gsharp Script Language (GSL).
The first job a script must do is declare the format of the file that it is creating. This is normally text/html but could also be something like image/gif.
#!/usr/sbin/perl print "Content-type: text/html\n\n"; print "<HTML>\n"; ... |
#!/bin/sh echo "Content-type: text/html"; echo echo "<HTML>" ... |
#!/bin/GsharpWE include "$UNIDIR/lib/libhtml.gsl"; HTTPheader(); HTMLheader(stdout,"This is the title","Advanced Visual Systems"); HTMLheading(stdout, 1, "This is my heading"); HTMLfooter(stdout); |
#!/bin/sh echo "Content-type: image/gif"; echo cat /usr/GsharpWE/example/GsharpWE/gscube10.gif |
How the script has access to the name/value pairs is dependent on the method specified in the form. If method=get (the default) then the pairs are attached to the end of the requested page.
/cgi-bin/process.cgi?name1=value1&name2=value2
The server places the pairs into the environment variable QUERY_STRING
If method=post then the script must read the pairs from stdin. This method is recommended for large numbers of pairs as some systems have a limit to the size of the environment.