TechMynd, Fresh Downloads and Latest Technology News, Stuff That Works | Downloads, Tips & Tricks, Softwares, Programming, Resources, IT, Computer, Resources, Freewares

Basics of a CGI Program



A CGI is simply a program that is called by the webserver, in response to some action by a web visitor. This might be something simple like a page counter, or a complex form-handler. Shopping carts and e-commerce sites are driven by CGI programs. So are ad banners; they keep track of who has seen and clicked on an ad.

CGI programs may be written in any programming language; we’re just using Perl because it’s fairly easy to learn. If you’re already an expert in some other language and are just reading to get the basics, here it is: if you’re writing a CGI that’s going to generate an HTML page, you must include this statement somewhere in the program before you print out anything else:

print “Content-type: text/html\n\n”;

This is a content-type header that tells the receiving web browser what sort of data it is about to receive — in this case, an HTML document. If you forget to include it, or if you print something else before printing this header, you’ll get an “Internal Server Error” when you try to access the CGI program.

Your First CGI Program

Now let’s try writing a simple CGI program. Enter the following lines into a new file, and name it “first.cgi”. Note that even though the lines appear indented on this page, you do not have to indent them in your file. The first line (#!/usr/bin/perl) should start in column 1. The subsequent lines can start in any column.

first.cgi - Hello World Program
——————————-

#!/usr/bin/perl -wT
print “Content-type: text/html\n\n”;
print “Hello, world!\n”;

Share/Save/Bookmark

Tags:
Posted in: CGI
Post's RSS » RSS 2.0
Post's Comments RSS » RSS 2.0

Related Posts


Post a Comment

Comment will appear here after approval, Thanks for patience

  Subscribe Via RSS
  Subscribe Via Email

Add to Technorati Favorites

About
Techmynd is created by Hiroshi, an IT enthusiast from Pakistan.

I love blogging, software development & social networking.

Ask me for solution relating to blogging, softwares, troubleshooting and get free advice/recommendation. I answer every email except spam.

Get Website Updates via Email:
more »






English flagItalian flagKorean flagChinese (Simplified) flagPortuguese flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flagCroat flagDanish flagFinnish flagHindi flagPolish flagRumanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flagSlovak flagSlovenian flagUkrainian flagVietnamese flag