What Are CGI Scripts and How Do They Improve Websites?
What Are CGI Scripts and How Do They Improve Websites
os
key os.environ:
key, , os.environ[key],
MUO
What Are CGI Scripts and How Do They Improve Websites
CGI stands for Common Gateway Interface. It is a standard protocol for web servers to execute programs and generate output for web browsers. Learn all about CGI scripts and how they can improve your website. CGI stands for Common Gateway Interface. It is a standard protocol for web servers to execute programs and generate output . The program is referred to as a CGI program (or ). It executes on behalf of the web server to produce output when a particular URL is requested by the user's web browser.Benefits of CGIs
A CGI enhances a website in many ways. Starting from authenticating a user to providing a full scale shopping application, CGIs are at the very core of making the web dynamic and interactive. The following are some of the main benefits of using CGIs to enhance the user experience. Personalized to the user: The output can be personalized to each user. For example, a user may be checking his bank account balances, which may need to be retrieved from the database. Other types of personalization: The user may be using a mobile phone to access the website, and the output needs to be adjusted for the mobile phone resolution. Making updates: An update is performed as a result of a form POST (explained below). For example, you may be updating your Facebook status. Uploading files: A file uploaded by a user needs a CGI on the server end to handle the file upload request to store the file in a particular location. Generating output: In addition to output displayed in the browser, many websites offer additional forms of output, such as Word Document, PDF, etc. Some of these documents may need to be generated on the fly by CGIs, such as your telephone bill or credit card statement.Serving Static Files vs CGIs
An (as well as and ) is served out directly by the web server. A CGI program, on the other hand, is executed by the web server when a particular URL is requested. It is used for fulfilling requests which need access to dynamic information, such as that residing in a database. An example would be authenticating a user to allow access: the username and password could be stored in a database, and a request to validate the user would need to access this information. For servicing this request, the web server would execute a CGI program that would lookup the information and validate the user.Configuring Web Servers
A web server runs HTTP server software which provides the ability to serve out HTML files, as well as execute CGI programs to generate output on the fly. The HTTP server software is configured by the web server administrator to execute specific programs when URLs matching specific patterns are requested.Executing CGIs
When a user's browser makes a request to a particular URL, the browser sends a whole bunch of information, in the form of HTTP headers and possibly the HTTP body. Some of this may include: The browser type The user's name if the user has logged in Any cookies previously stored by the web server Form variables if the request is a POST request When a CGI is executed to fulfil this request, the server passed on all this information to the CGI program. The CGI may use some or all of this information to handle the request and personalize the output to the user.Handling Form POSTs
A primary use of CGIs is to process HTML forms. A HTML form is a user-interaction device and provides a variety of controls so the user can provide input. When the user clicks a Submit button (or something similar) on the form, the web browser aggregates the information in the form and sends it to the target URL indicated in the form. The web server, in turn, typically invokes a CGI program and sends all the input collected from the user. This interaction is called a form POST, a technical term indicating the method of sending the form contents to the CGI. The CGI processes the form contents and produces a suitable response to be sent to the browser.A Sample CGI
The following is a sample CGI program . It can be deployed to a web server so that it is accessible from a URL similar to: http://www.example.com/cgi-bin/env.cgi. When invoked, the CGI reports all the variables it received in its environment from the web server. This is about as simple as a CGI program can get.os
key os.environ:
key, , os.environ[key],