What is CGI programming?
CGI (Common Gateway Interface) is a standard protocol that lets a web server execute a program and pass the output back to the browser as an HTTP response. The server invokes a script, typically written in Perl, Python, or shell, sets a defined set of environment variables describing the request, and reads the program's standard output as the response body. CGI was the dominant form of dynamic web content from the mid-1990s through the early 2000s, and millions of sites still run CGI scripts today on shared Linux hosts.
Is CGI still used in 2024?
Yes. Shared hosting environments on Apache and LiteSpeed typically have a /cgi-bin directory enabled by default, and many legacy applications written in Perl or shell have never been rewritten. CGI is also the simplest way to add server-side logic to a static site on a traditional host without installing a framework. Modern alternatives like WSGI (Python) and FastCGI reduce per-request overhead, but CGI remains widely deployed and well supported.
Which languages can you use to write CGI scripts?
Any language that can read environment variables and write to standard output works as a CGI script: Perl, Python, Ruby, Shell (Bash), C, PHP in CGI mode, and others. Perl dominated the early web era because of its mature text-processing libraries and the CGI.pm module. Python is now equally common and cleaner to read. Shell scripts work for simple form-to-email tasks. The interpreter must be installed on the server and the script must be marked executable.
How do I send form data to a CGI script?
An HTML form with method='POST' and action pointing to your CGI script URL will POST the form body to the script. The server sets CONTENT_LENGTH and makes the data available on standard input. A Perl script using CGI.pm or a Python script using the cgi module can parse the fields with a single call. GET requests pass data in the QUERY_STRING environment variable instead. Always validate and sanitize every field server-side before using it.
What is the correct shebang line for a CGI Perl script?
The shebang line must point to the exact Perl interpreter on the server: #!/usr/bin/perl is correct on most Linux hosts, while some systems use #!/usr/local/bin/perl. Always add 'use strict;' and 'use warnings;' on the next two lines. To help diagnose configuration errors, add 'use CGI::Carp qw(fatalsToBrowser);' during development so error messages appear in the browser rather than the server error log.
Why am I getting a 500 Internal Server Error from my CGI script?
The most common causes are: the script is not marked executable (chmod +x script.cgi), the shebang path is wrong for the server, there is a syntax error in the script, the Content-type header is missing or malformed, or the script is printing output before the header. Check the server's error_log for the exact message. CGI::Carp fatalsToBrowser is useful during development, but remove it in production since it can expose internal paths.
How do I make a CGI file upload script?
In Perl, CGI.pm handles multipart/form-data uploads through its upload() method. In Python, the cgi module's FieldStorage class gives you a file-like object for the uploaded content. The key requirements are: the form must use enctype='multipart/form-data' and method='POST', the script must validate file type and size before processing, and the file must be written to a directory that is not web-accessible. Never trust the filename supplied by the client.
What is a CGI form handler?
A CGI form handler is a server-side script that receives HTML form submissions, validates the input, processes it (typically sending an email or writing to a file), and returns an appropriate HTML response. Classic examples include contact forms, feedback forms, and newsletter signups. The handler reads POST data, validates each field against expected patterns, uses a mail library or sendmail to dispatch the message, then prints a thank-you page or redirects with a Location header.