CGI - Common Gateway Interface

CGI - Common Gateway Interface, Part 1: Making and Running a CGI Script

When the World Wide Web was getting started, people were thrilled by the new way to get information from the Internet. For a while, there weren't many websites, and the ones which were there didn't do much other than deliver pages to your screen. But technology inevitably moves forward, and as the Web grew, and people started putting it to incrasingly creative purposes, there arose the problem of how users could provide any information to sites. How could a search engine work, for example, unless the user had some way of telling it what to search for? It wasn't long, then, before CGI was developed.

As you've probably guessed, CGI is just a way for users to provide info for websites. It usually takes the form of input fields, where you can type into little boxes, like most search engines use. CGI works by letting the website run a program to process the information you provide. CGI is not a programming language in itself, it is only a way for the web server to run programs. The program is put into a CGI "script", which is actually just a normal program listing with a few CGI codes added. CGI will support any language, as long as there is an interpreter for that language (NOT a compiler) on the server. However, Perl is the preferred language for making CGI scripts.

So what do you put into a CGI script? As an example, here's a simple Perl CGI script:


print "Content-Type: text/html\n\n";
print "Hello, world!!";

This CGI script doesn't actually take any user input. It just prints the ubiquitous "Hello, world" message, and in fact the result looks just like a normal web page. It's just used for example here. Anyway, the second two lines are nothing more than normal Perl code, the first one indicates that it's about to produce HTML (in other words, a website), and everything after that is the page content. So you see, the CGI script is just a normal Perl program except for the first line.

And what about that first line? That is what's sometimes called the "shebang line". It must always be the first line of a CGI script, and it simply says where a Perl interpreter on the Web server. It always begins with the #! and then just comes the path to the interpreter that's to be used for processing the rest of the CGI script. (Note that Perl is by nature an interpreted language, which is why it's popular for CGI. C and most other popular programming languages are usually compiled.) Unix people will recognize that "/usr/local/bin/perl" is Unix path format. For DOS/Windows, use the usual "c:\perl\perl.exe", modifying to point to the actual location of your Perl interpreter, of course.

So, now we have a CGI script. How do we run it? Well, for starters, CGI scripts have to be run from the /cgi-bin directory of a Web server. If you have your website on a server run by someone else other than you, check if they allow users to put their own scripts into /cgi-bin. If they don't, you're out of luck and can forget about running your own CGI on that server anyway. If they do, put the script in there, and then anyone can run it just by going to that file. For example, if the web server's name is and the filename of the CGI script is, you'd run it by going to

If you're running your own Web server, then just drop the script into cgi-bin and run it the same way.

So, now you know how to make a CGI script and run it. The next question is, how do you make and use the user-input fields CGI was made for in the first place? For this, go to Making a CGI form.

Back to the main page