Writing CGI Programs And Scripts
What are CGI Programs ?
Like most other types of programs CGI programs can be sent a set of arguments when they are started. This allows you to feed in various parameters to a CGI program. For instance a Search CGI program would probably need the word or words you want to search for. A Form Processing CGI needs to receive all the data fields on a web form so it can process the information.
Environment Variables Environment Variables are values that the operating system stores and retrieves for a programs use. In the case of CGI programs they are used for communicating various useful information between the web server and the CGI program.
Environment Variables Environment Variables are values that the operating system stores and retrieves for a programs use. In the case of CGI programs they are used for communicating various useful information between the web server and the CGI program.
To see a complete list of environment variables Click Here.
Web Forms whose action property is set to GET send the form data to the CGI program thru the "QUERY_STRING" environment variable. The form data is URL encoded at the end of the requested URL. There is a Question Mark Character "?" that divides the URL and the query string. The text after the "?" is the form data URL Encoded. URL encoding replaces all space characters with + characters and replaces some other special characters with hexadecimal notations.
Standard Input Web Forms whose action property is set to POST send the form data to the CGI program thru what is called Standard Input. The form data is sent encoded in a MIME format. In order to receive this data the CGI program must read all the data available in the Standard Input buffer. This is usually done in the same way as reading keyboard input to a program. If a programmer wants to know explicitly how many bytes of data have been sent to a CGI program he can find out by asking the system for the "QUERY_STRING" environment variable.
Most CGI programs use some type of CGI Library to parse the encoded parameters that the CGI program was called with out of the "QUERY_STRING" environment variable or the data on the Standard Input. Click Here For More Information.
Sample CGI programs
Here are some sample CGI programs and perl scripts. Each one is listed in its C source code and its perl source code so you can see the similarities. In order to run the C CGI samples you would need to compile them into binary executable programs using a C compiler like Visual C++ from Microsoft or the free GNU C++ or G++ compilers. In order to run the perl samples you must install perl on your system. A Windows version of perl is available for free from Active State. A compiled binary version of each of the C samples is also provided for your convenience.
Running A Sample CGI program
If you want to try running a pre-made CGI program:
- Download the HelloWorld.exe sample CGI.
- Place it in your cgi-bin directory which can be found in your server root directory.
- Make sure "Use Enable /cgi-bin directory" is enabled on the Server Properties Page.
- You should be able to run the CGI with the following URL:
http://localhost/cgi-bin/HelloWorld.exe
- You should see "Hello World" displayed on a web page if it is working.
Running A Sample CGI PERL Script
If you want to try running a pre-made CGI program:
- Make sure perl is installed on your computer you can get a windows build from Active State.
- Download the HelloWorld.cgi sample CGI.PERL script.
- Place it in your cgi-bin directory which can be found in your server root directory.
- Make sure "Use Enable /cgi-bin directory" is enabled on the Server Properties Page.
- Click Script on the Server Properties Page and make sure there is a mapping for cgi to the perl interpreter perl.exe. You should enter the complete path to perl.exe unless you have perl's location in your computers path statement.
- You should be able to run the CGI from your computer with the following URL: http://localhost/cgi-bin/HelloWorld.cgi
- You should see "Hello World" displayed on a web page if it is working.
Where Do You get Free Scripting Engines
You can download and install several free scripting engines (interpreters) here are just a few.
Where Do You Go From Here ?
Check out this web site's Tools Section for sources of both pre-written CGI programs and libraries you can use to write your own CGI programs and scripts. Check out this web site for really basic generic information on CGI.
|