What is CGI, and what do I need to know about it to program in it?


CGI stands for “Common Gateway Interface”, and is simply a way for a web server to invoke a computer program to interact with a web browser.  The cgi script accepts input in the form of “name value pairs” (similar to perl’s hashes mentioned above), and allows you to print output back to the browser in any manner you like, using print statements (if using perl of course – you can write CGI in any language you like).


CGI is great for smaller tasks not requiring a large amount of traffic to be pumped through them.  It is also available on the largest number of web hosting providers, and relatively easy to use.  Use mod_perl, C w/ the apache API, PHP, ASP, JSP, Cold Fusion, Zope, or similar technology when working with super massive high traffic sites.  Hosting for these high performance platforms can be prohibitively expensive for up and coming businesses however.  And unless you’re getting 20+ requests a second on your cgi scripts, you probably won’t notice any slowness.  That number of 20 of course varies wildly based on the hardware and network capacity of your web hosting situation.


Here’s a diagram to help you better understand CGI:



And here’s what the 11 steps mean:


1. The web surfer fills out a form, and clicks submit. The information in the form is sent over the internet to the web server.
2. The web server "grabs" the information from the form, and passes it to the CGI Software.
3. The CGI Software then performs whatever validation of this information that is required. For instance, it checks to see if an email address is valid. If this is a database program, the CGI Software prepares a database statement, to either add, edit or delete information from the database.
4. The CGI Software then executes the prepared database statement, which is passed to the database driver.
5. The database driver acts as a middleman, and performs the requested action on the database itself.

6. The results of the database action are then passed back to the database driver.
7. The database driver sends the information from the database to the CGI Software.
8. The CGI Software takes the information from the database, and manipulates it into the format that is desired.
9. If any static html pages need to be created (or similar task needs to be performed), the CGI program accesses the web server computer's file system, and reads, writes, and/or edits files.
10. The CGI Software then sends the result it wants the web surfer's browser to see back to the web server.
11. The web server sends the result it got from the CGI Software back to the web surfer's browser.



When you create an html form like this:



<form action=”/cgi-bin/myscript.cgi” method=”post”>

<p>Your name: <input type=”text” size=”20” name=”your_name”><br>

Your email address: <input type=”text” size=”50” name=”email_address”>

<input type=”submit” value=”Print out my name and email!”>





You can choose either “get” or “post”.  Only use get in situations where you need the user to be able to bookmark the resulting page.  Otherwise, use post.  Using get you can only have 1024 bytes sent to the program, which in a lot of cases isn’t enough data.


When the user submits this form, the data made available to your script looks something like this: your_name=David+Nelson&email_address=david@somehost.com&x=54&y=102


Actually, because of url encoding, it may look different depending on what browser you send it from.  But that’s not important.  In smart cgi programming, we use what’s called a “library” or “module” which shields us from doing the hard stuff like splitting up that string that we get from the form.


So, we use our library to grab the value of the inputted data, then send what’s called an “HTTP Header” back to our browser, then we type html code in our perl script to mark up the data we want to send back to the browser.


In a nutshell, that’s all there is to cgi.  In the next chapter, I get into the exact functions that our favorite perl/cgi library, called “CGI.pm” does for us.  It does many, many great things, including splitting up the data we get from the form into a very easily usable format for us, automatically!  I also touch on another useful module, which let’s us talk to databases.


<-- Previous: Accessing The File System Using Perl | Next: Using CPAN modules to save coding time/complexity, namely CGI.pm -->

Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:

Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows

Link To
This Page!

Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script