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:
When you create an html form like this: <html> <body> <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!”> </form> </body> </html> 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: | ||||