0% found this document useful (0 votes)
51 views

CGI Notes

The document discusses Common Gateway Interface (CGI) which allows a web server to interface with external programs run on the server. CGI scripts are invoked by the HTTP server, usually to process user input submitted through HTML forms. There are two main methods for submitting data from a browser to a CGI script: GET and POST. GET appends the data to the URL while POST sends it as a separate message to the script's standard input. File uploads also use POST and require the form's enctype to be set to multipart/form-data. CGI scripts have access to environment variables that provide information about the request.

Uploaded by

Somya Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

CGI Notes

The document discusses Common Gateway Interface (CGI) which allows a web server to interface with external programs run on the server. CGI scripts are invoked by the HTTP server, usually to process user input submitted through HTML forms. There are two main methods for submitting data from a browser to a CGI script: GET and POST. GET appends the data to the URL while POST sends it as a separate message to the script's standard input. File uploads also use POST and require the form's enctype to be set to multipart/form-data. CGI scripts have access to environment variables that provide information about the request.

Uploaded by

Somya Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIT-4

COMMON GATEWAY INTERFACE

Common Gateway Interface is a set of standards which explains how information


or data is exchanged between the web server and a routine script. This interface is
used by web servers to route information requests supplied by a browser. CGI is
customary for external gateway programs to interface with information servers
such as HTTP servers. A CGI script is invoked by an HTTP server, usually to
course user input which is submitted through an HTML <FORM> or an
<ISINDEX> element.

On clicking on a hyperlink to browse a particular web page or URL, browser


interacts with the HTTP web server and asks for the same URL (or filename). Web
Server parses the URL and looks for the same filename. If that file is found, then
that file is sent back to the browser, otherwise, an error message is sent indicating
that we are demanding the wrong file. Web browser takes the response from a web
server and displays it, then whether it is the received file from the webserver or an
error message. Conversely, it is possible to set up the HTTP server so that
whenever a specific file is requested, then that file is not sent back, but instead, it is
executed as a program, and whatever that program output is, that is sent back to
our browser for display. This same function is called the Common Gateway
Interface (or CGI) and the programs which are executed are called CGI scripts.

1. Architecture of CGI
2. CGI Environment Variables

All the CGI programs have access to the following environment variables. These
variables play an important role while writing any CGI program.
Sr. No. Variable Name & Description

1 CONTENT_TYPE: The data type of the content. Used when the client is sending
attached content to the server. For example, file upload.
2 CONTENT_LENGTH: The length of the query information. It is available only for
POST requests.
3 HTTP_COOKIE: Returns the set cookies in the form of key & value pair.

4 HTTP_USER_AGENT: The User-Agent request-header field contains information


about the user agent originating the request. It is name of the web browser.

5 PATH_INFO: The path for the CGI script.

6 QUERY_STRING: The URL-encoded information that is sent with GET method


request.

7 REMOTE_ADDR: The IP address of the remote host making the request. This is
useful logging or for authentication.

8 REMOTE_HOST: The fully qualified name of the host making the request. If this
information is not available, then REMOTE_ADDR can be used to get IR address.

9 REQUEST_METHOD: The method used to make the request. The most common
methods are GET and POST.

10 SCRIPT_FILENAME: The full path to the CGI script.

11 SCRIPT_NAME: The name of the CGI script.

12 SERVER_NAME: The server's hostname or IP Address

13 SERVER_SOFTWARE: The name and version of the software the server is


running.
3. GET and POST Methods
A user must have come across many situations when there is a need to pass some information
from the browser to web server and ultimately to CGI Program. Most frequently, browser uses
GET Method and POST Method to pass this information to web server.

3.1 GET method


The GET method sends the encoded user information appended to the page request. The page
and the encoded information are separated by the ? character.

 The GET method is the default method to pass information from the browser to the web
server and it produces a long string that appears in your browser's Location:box.

 Never use GET method if you have password or other sensitive information to pass to the
server.

 The GET method has size limitation: only 1024 characters can be sent in a request string.

 The GET method sends information using QUERY_STRING header and will be accessible
in your CGI Program through QUERY_STRING environment variable.

Example:

/cgi-bin/hello_get.py?first_name=TMU&last_name=UP

3.2 POST Method


A generally more reliable method of passing information to a CGI program is the POST method.
This packages the information in exactly the same way as GET methods, but instead of sending
it as a text string after a ? in the URL it sends it as a separate message. This message comes into
the CGI script in the form of the standard input.

4. File Upload
To upload a file, the HTML form must have the enctype attribute set to multipart/form-data.
The input tag with the file type creates a "Browse" button.
<html>

<body>

<form enctype = "multipart/form-data" action = "save_file.py" method = "post">

<p>File: <input type = "file" name = "filename" /></p>


<p><input type = "submit" value = "Upload" /></p>

</form>

</body>

</html>

You might also like