Common Gateway Interface (CGI) is a method for Web servers to process and run executable scripts, or programs, and generate dynamic content for display in a...
Common Gateway Interface (CGI) is a method for web servers to process and run executable scripts or programs and generate dynamic content for display in a user's web browser.
What Is CGI?
CGI was designed to allow web browsers to run executable scripts. Under normal circumstances, when a web browser requests a webpage or file, the HTTP server simply serves up the requested file for display or download.
However, there are circumstances when simply displaying or downloading a file isn't the desired outcome. For example, when filling out and submitting a form, the goal is to have the form contents emailed to the website operator while providing the user confirmation that the form was submitted successfully.
This is where CGI comes into play. A site administrator can specify a folder to contain all executable scripts—commonly named "cgi-bin." When a web browser requests a URL pointing to one of these scripts, the HTTP server runs the script and returns the results, rather than serving up the script file itself.
Common Uses
CGI scripts serve various purposes:
Form Processing
A common use of CGI scripts is submitting form data. Scripts can collect the data, save it in a database, email it to the website operator, and display an acknowledgment to the person submitting the form.
Interactive Features
CGI scripts can also be used for:
- Signing a guestbook
- Submitting search queries
- Processing translation forms
- Generating dynamic content based on user input
How It Works
- User submits a request (like a form)
- Web server identifies the request points to a CGI script
- Server executes the script
- Script processes the data
- Script returns output to the server
- Server sends the response to the user's browser
While CGI was one of the earliest methods for creating dynamic web content, modern web applications often use more advanced technologies. However, understanding CGI provides foundational knowledge about how web servers process dynamic content.
