WWW FAQ: My CGI scripts don't work. How can I debug them?

My CGI scripts don't work. How can I debug them?

Several common causes are described here. Note that every web server is different; your mileage will almost certainly vary. In particular, Windows and Macintosh servers differ drastically from Unix servers. See your server's documentation.
The Server Must Recognize Your Program
Simply linking from your page to an executable program or script won't cause it to be run by the server. There are two common arrangements: either files in directories specially designated by the server administrator are executed as CGI scripts, or files with a special extension (such as .cgi) are executed as CGI scripts.

These are just two possible ways your server might be configured. Many sites don't allow users to run CGI scripts at all. Consult your web server's administrator.

Always Output a MIME Type
Every CGI script must output a MIME type indicating what kind of document it is producing. If your script outputs an HTML page, the correct format is:

Content-type: text/html

Followed by two line feeds (ascii 10 decimal).

After the MIME type, output the desired HTML.

Always Flush Output
On many systems, unexpected problems can result when a CGI script outputs a MIME type, then executes another program to generate output. To prevent such problems, flush standard output before executing other programs. If your script is written in C, the proper code is usually: fflush(stdout);
Permissions and Paths: Why Can't My Script Access My Files?
CGI programs typically execute with a current directory and user ID that differ from your personal home directory and user ID. When you write CGI programs, make sure any files accessed are accessed by absolute path (beginning from the root of the file system). Also, users of multiuser systems such as Unix may have to grant all users read access or even write access to data files using the chmod command. This is not an ideal situation. Better servers run your CGI programs using your user ID. Talk to your admin if you have difficulties in this area.
When One Browser Works and Another Doesn't
Some browsers are tolerant of incorrect Content-type headers, as well as of null characters in text/html or text/plain output. Make sure your output is strictly correct; it helps to check the script with Netscape, Mosaic and Lynx.

World Wide Web FAQ