How to Use CGI Scripts in Web Hosting

CGI means Common Gateway Interface. It's a way to provide interactivity to Web pages, in particular to handle the input from forms. For instance, you can use CGI to take information from a form and send it to your e-mail account, and many shopping-cart programs use CGIs. Many Web-hosting companies have libraries of CGI scripts you can use. Some allow you to install your own CGI scripts, but don't provide a library. Others don't allow you to add any CGIs.

A standard for interfacing web servers with an executable application.  A CGI program can be written in any language like Perl or C/C++ and it is often stored in a special directory like /cgi-bin.  CGI is often used to process data from HTML forms.

What are CGI Scripts?

The difference between your web site just being a virtual billboard and it being an interactive web site, is having the ability to get information to and from your viewers. The Web provides a mechanism for accomplishing this, CGI, the Common Gateway Interface, using CGI scripts. An example of a CGI script would be: Shopping cart systems, hit counters, guestbooks, order forms, mail, maps, etc.

CGI scripts are the way most servers communicate with other programs and scripts. CGI scripts are simple text files, lines of code, that are interpreted as requested by the server. A CGI script will work together with other programs and with the HTML content of your web page. Since HTML alone only allows for information to be displayed, CGI scripts give HTML the ability to interact with the visitor. CGI scripts can be written in Perl, PHP, C, Visual Basic, or Python. CGI is not a programming language itself. It's more of a protocol for communication between the web server and the script. Perl and PHP seem to be the languages of choice, because they are easy to learn and are very portable.

How Does A CGI Script Work?

A CGI Script is a collection of lines of code that contain instructions for the server. Unlike a simple HTML document request, a CGI-BIN (Common Gateway Interface) request requires something a whole lot smarter than a regular HTML request for content from the server. When you click on a link that looks like: http://100best-web-hosting.com/cgi-bin/Guestbook/guestbook.cgi, you are really instructing the server to run an actual program that handles only this one type of request. In this case it runs the program that calls up the form that allows you to make an entry in the guestbook. Here is a breakdown of what actually happens:

Your browser sends the request shown above to the server.

The server runs the guestbook.cgi program.

The guestbook.cgi program realizes that you are not adding anything yet, so it makes the decision to create and return to you, a blank data entry form.

Your browser then displays this form as it would any other HTML document.

So you take a moment and fill in all the fields and click on the SUBMIT button. Now a more complex series of actions take place. The command in the browser location window may look something like this:
/cgi-bin/Guestbook/guestbook.cgi?name=joe&[email protected]

When the CGI receives the request, it executes a program called guestbook.cgi that is located in the Guestbook directory under cgi-bin. What is different now is that in addition to a program request, information is being fed to the CGI script.

This is what the script then does:

Load the guestbook.cgi program into memory for running.

It notices that it has to split up the information using the & as a way of telling where one bit starts and ends.

Seeing that the e-mail has a name in it, sends a thank you note to the person that signed the book.

Sends a note to the webmaster that a new entry is in the guestbook.

Takes all the input data, and uses the information to create a new entry that is tagged onto the guestbook.html document.

Creates an HTML document saying "Thank you for signing my guestbook!" that is returned to the browser. And the CGI's job is finished.

The important concept is that in order to use CGI-BIN or a CGI script, you must create a real, however big or small computer program. It is not embedded in your HTML document, but it is installed in your cgi-bin directory located on your server.

What are Free CGI Scripts?

Free CGI scripts are prewritten applications in PERL or other programming code, that you can download or copy straight from the provider. By utilizing the many free CGI scripts available you can have CGI features on your site (guestbook, forms, etc.), but don't need any programming skills to create these features on your own. Usually the free CGI scripts will run from the CGI providers servers, so you don't have to deal with complex installation and maintenance procedures. Free CGI scripts like hit counters and guestbooks are offered by most web hosts. If your web host doesn't offer these features, here are a few resources for Free CGI Scripts: HotScripts www.hotscripts.com, The CGI Resource Index www.cgi-resources.com, Free-Scripts.Net www.free-scripts.net, Script Search www.scriptsearch.com.

Do I need CGI Scripts in the hosting package I choose?

In order to use CGI programs, your server must be configured to support them. Check with your system administrator as to whether or not you can use them, and if so, where your CGI programs must reside. If you are not allowed to do CGI on your server- consider moving to someplace where you can, or finding somebody to host your scripts.