Error Handling and Output

As we saw in the last example, output to standard out in a CGI program goes to the web server, and is sent on to the browser. Standard error however is handled differently. For standard error, the output is sent to the web server error log, which in Apache is the "error_log" file. Both the access_log and error_log file live in the logs directory under the httpd configuration directory, typically /etc/httpd. If a program wants to send error output to the browser, it will need to send the message to standard out, but will also typically send a error response code. Depending on the error, the programmer may want to also write to standard error in order to document the error for debugging purposes.

In order to tell the web browser that the response is an error, the CGI program can set a response code with the error text. Some of these codes will be familiar to web surfers, such as the "Error 404" which means that the requested document does not exist. Some common response codes are:

(click here for a complete list)

In addition to sending these response codes to the web browser, the web server will also include the error codes in the access.log file, which can be useful for system monitoring purposes.


There is no pleasing this example program. If you send it input in a query string, it will complain that it didn't want input. If you don't send it input, it will also complain. I'll also show an example of how a browser may intercept an error code. These links get their input using URL encoding on the link that invokes them. URL encoding will be discussed next.

Next Previous Examples

Copyright 2001 - Andy Welter