About MartNet MartNet Technical Support
Home Services Support Members Software Contact
Search Support
MartNet WebMail

MartNet WebAdmin

Dialup Access

Domain Name

MartNet News /
  Status Info

NOC / System Status

Support Topics:
 MartNet WebAdmin
 Virtual Hosting
 Web Site Construction
 Unix / Linux
 Internet Chat
 Game Server Services
 Misc. Stuff
 Policies / Legal

MartNet Ad

 MartNet WebAdmin
 Virtual Hosting
 Game Server Services
 Misc. FAQ's

 Privacy and Security
 Web Development
 Unix / Linux
 Game Server Services
 Misc. Support Links

MartNet Policies
Billing Dept.

Check Domain

Advanced Web Site Development
An introduction to writing CGIs

So you really need to throw some neat cgi's into your page in order to really spruce the site up. It's really not that hard, contrary to what many professional programmers may tell you. If you can read from STDIN and write to STDOUT, then you can write CGI scripts. If you're a programmer already, this primer can teach you the basics of CGI in a few minutes. If you're not a programmer, this primer won't help you much-- sorry. Learn some programming, even shell scripting, and come back when you're done. Good luck!

What is CGI?

CGI is not a language. It's a simple protocol that can be used to communicate between Web forms and your program. A CGI script can be written in any language that can read STDIN, write to STDOUT, and read environment variables, i.e. virtually any programming language, including C, Perl, or even shell scripting.

Structure of a CGI Script

Here's the typical sequence of steps for a CGI script:

1.Read the user's form input.
2.Do what you want with the data.
3.Write the HTML response to STDOUT.

The first and last steps are described below.

Reading the User's Form Input

When the user submits the form, your script receives the form data as a set of name-value pairs. The names are what you defined in the INPUT tags (or SELECT or TEXTAREA tags), and the values are whatever the user typed in or selected. (Users can also submit files with forms, but this primer doesn't cover that.)

This set of name-value pairs is given to you as one long string, which you need to parse. It's not very complicated, and there are plenty of existing routines to do it for you. Here's one in Perl, a simpler one in Perl, or one in C. For a more elaborate CGI framework, see Perl's CGI.pm module. The CGI directory at Yahoo includes many CGI routines (and pre-written scripts), in various languages.

If that's good enough for you, skip to the next section. If you'd rather do it yourself, or you're just curious, here's the format of the long string:


So just split on the ampersands and equal signs. Then, do two more things to each name and value:

1.Convert all "+" characters to spaces, and
2.Convert all "%xx" sequences to the single character whose ascii value is "xx", in hex. For example, convert "%3d" to "=".

This is needed because the original long string is URL-encoded, to allow for equal signs, ampersands, and so forth in the user's input.

So where do you get the long string? That depends on the HTTP method the form was submitted with:

For GET submissions, it's in the environment variable QUERY_STRING.
For POST submissions, read it from STDIN. The exact number of bytes to read is in the environment variable CONTENT_LENGTH.

(If you're wondering about the difference between GET and POST, see the footnote discussing it. Short answer: POST is more general-purpose, but GET is fine for small forms.)

Sending the Response Back to the User

First, write the line

Content-type: text/html

plus another blank line, to STDOUT. After that, write your HTML response page to STDOUT, and it will be sent to the user when your script is done.
That's all there is to it.

Yes, you're generating HTML code on the fly. It's not hard; it's actually pretty straightforward. HTML was designed to be simple enough to generate this way.

If you want to send back an image or other non-HTML response, here's how to do it.

That's it. Good Luck!

See how easy it is? If you still don't believe me, go ahead and write a script. Make sure to put the file in the right place on your server, and make it executable; see this footnote for more hints.

Before you write too many scripts, learn about CGI security issues.

Oh yeah, CGI stands for Common Gateway Interface-- in other words, a standard ("common") way of communicating ("interface") between processes on different machines ("gateway", sort of).

Other Resources:

The CGI Resource Index- http://cgi.resourceindex.com/
Matt's Script Archive- http://www.scriptarchive.com/
Using CGI's at Martnet - http://support.martnet.com/content.php?get=66

This Web Site is proudly built upon an Open Source foundation:
Assembled with PHP Powered by Debian Linux Fueled by MySQL
Served by Apache

Home | Services | Support | Members | Software | Contact

MartNet - PO Box 42472 Philadelphia, PA 19101
Copyright 1996 - 2015 MartNet Communications LLC. All Rights Reserved.