ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Internet & the Web

How CGI-BIN works | Working of CGI-BIN |Steps in CGI | Process of CGI Scripts

Updated on January 19, 2011

Working of CGI

Working of CGI
Working of CGI


CGI stands for Common Gateway Interface. CGI is a set of standards by which Web Servers can communicate with external program to handle the data from the form. This is why web servers are not programmed to process the forms. CGI doesn’t communicate directly with the browser. CGI-BIN acts as an interface for the CGI. CGI can be created using the programming languages C,Perl,Java or Visual Basic. CGI normally resides in a directory and this directory is known as CGI-BIN.

Working of CGI present inside CGI-BIN

·         A CGI is a program which is generally called scripts because they were written in UNIX and shellscript and Pearl.

·         Pearl is an interpreted language in which instruction are interpreted and immediately compiled to machine instructions.

·         C language is also used to create CGI-Bin but it is not called as a script.

·         After looking at CGI program server access information from browser. Before launching the scripts it prepares a no of variables called environment variables which will represent te current state of a server. If one sever is busy with various scripts, CGI executes its scripts at once each with its own environment.

How CGI works | Steps in CGI process with CGI-BIN

1. Browser will first show the document containing the form.

2. User will enter data into the form and click submit button.

3. Script in browser will validated the data entered in the form (client-side validation).

4. Browser will decode the URL and contacts the server.

5. Browser will request the document file from the server.

6. Server translates the URL into a path and a file name.

7. Server realizes that the URL points to a program.

8. Servers prepare its environments variables and launch the scripts.

9. CGI Script executes and read the environment variables and STDIN.

10. Script sends appropriate MIME header to STDOUT for content.

11. Script sends rest of its output to STDOUT and terminates.

12. Server will notify the termination of the script and closes the connection to browser.

13. Browser displays the output from the CGI script.

STDIN: It is a mnemonic for standard input. The input is redirected to a program from browser via server when POST method is used.

STDOUT: It is a mnemonic for standard output. It writes the output back to the browser.


·         Requests for data and is used to obtain static documents.

·         The requested information will appended onto the end of the URL as a query string.

Eg : cgi_bin/myprog.cgi?name=tom&id=32109

Query strings here used are: name=tom and id-32109

·         If slash is used instead of question mark the server passes info to script using PATH_INFO variable instead of QUERY_STRING.


·         When browser send data from a form using STDIN this method is used.

·         The server passes one variable called CONTENT_LENGTH to indicate the size of the incoming data in bytes.

These 2 methods GET and POST are passed as parameter to the server by a variable REQUEST_METHOD.

Steps in CGI process

CGI process
CGI process

CGI process

There are 3 steps in CGI processing

1.       Initialization

2.       Processing

3.       Termination


·         CGI Script determines the input, environment and the state.

·         Each time it acts like as it was invoked for the first time.

·         The method used to invoke the script can be determined using REQUEST_METHOD variable.

·          The input can be retrieved using the following variables

·          If request method is GET, QUERY_STRING variable can be used to determine the input.

·         If request method is POST, STDIN and CONTENT_LENGTH variable can be used to determine te input.


Processing phase is divided into 2 phases

1.       Processing input data

Transforms the data which differentiates it from all the CGI programs.

2.       Out put the results

Output is usually a header and some HTML in case of simple CGI scripts.


·         It refers to cleaning up resources after processing and quitting.

·         All files, memory allocated must be freed or released for other useful purpose. Failure to do this will report an error.

·         An error exit routine can be used to check whether the scripts resources are cleaned up as thoroughly as possible.

CGI Environment Variables

Environment Variables represent the current state of the server. Each server has its own environment variables and most commonly used are:

AUTH_TYPE provides type of authentication supported by the server.

CONTENT_LENGTH gives the length of data supplied by STDIN and POST method.

CONTENT_TYPE is type of data just as MIME header.

HTTP_ACCEPT is a common delimited list of MIME types accepted by browser.

PATH_INFO is extra path information supplied at the end of URL.

Eg: cgi_bin/mypage.cgi/dir1/dir2.

GATEWAY_INTERFACE is the version number of CGI ex:CGI/1.1

QUERY_STRING is extra information at the end of the URL after ‘?’

Eg: cgi_bin/mypage.cgi?name=Tom.

REQUEST_METHOD is method by which CGI script is invoked.

Eg: POST and GET

SCRIPT_NAME is the name of the script being invoked.

Eg: mypage.cgi

REMOTE_HOST is the host name of client making the request.

REMOTE_USER is the user name provided by the user if any AUTH_TYPE is supported.

SERVER_PORT is the port number for the connection.

Eg: 8080

SERVER_PROTOCOL is a name/ version of protocol used by request.

Eg: http/1.0

Normally when you are logging to your email account watch the URL tab of the browser we can see the request is going to the cgi_bin. CGI program or CGI script resides in this cgi_bin directory. CGI-BIN acts as the interface for CGI programs. Using of CGI reduced the complexity for building servers as well as browsers. The server should not do the input processing it will call the CGI program present inside the CGI-BIN.


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: ""

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)