eekim.com > Publications > CGI Developer's Guide > Chapter 2

Chapter 2: The Basics    (01)

<Next | Table of Contents | Previous>    (02)

Hello, World!    (03)

You begin with the traditional introductory programming problem. You want to write a program that will display Hello, World! on your Web browser. Before you can write this program, you must understand what information the Web browser expects to receive from CGI programs. You also need to know how to run this program so you can see it in action.    (04)

CGI is language-independent, so you can implement this program in any language you want. A few different ones are used here to demonstrate this language independence. In Perl, the "Hello, World!" program looks like Listing 2.1.    (05)


Listing 2.1. Hello, World! in Perl.    (06)


#!/usr/local/bin/perl
# hello.cgi - My first CGI program
print "Content-Type: text/html\n\n";
print "<html> <head>\n";
print "<title>Hello, world!</title>";
print "</head>\n";
print "<body>\n";
print "<h1>Hello, world!</h1>\n";
print "</body> </html>\n";    (07)

Save this program as hello.cgi, and install it in the appropriate place. (If you are not sure where that is, relax; you'll learn this in "Installing and Running Your CGI Program," later in this chapter.) For most people, the proper directory is called cgi-bin. Now, call the program from your Web browser. For most people, this means opening the following Uniform Resource Locator (URL):    (08)


http://hostname/directoryname/hello.cgi    (09)

Figure 2.2. Your first CGI program, if all goes well, will display Hello, world!.    (011)

hostname is the name of your Web server, and directoryname is the directory in which you put hello.cgi (probably cgi-bin). Your Web browser should look like Figure 2.2.    (010)

Dissecting hello.cgi    (012)

There are a couple of things worth mentioning about hello.cgi. First, you're using simple print commands. CGI programs do not require any special file handles or descriptors for output. In order to send output to the browser, simply print to the stdout.    (013)

Second, notice that the content of the first print statement (Content-Type: text/html) does not show up on your Web browser. You can send whatever information you want back to the browser (an HTML page or graphics or sound), but first, you need to tell the browser what type of data you're sending it. This line tells the browser what sort of information to expect—in this case, an HTML page.    (014)

Third, the program is called hello.cgi. It's not always necessary to use the extension .cgi with your CGI program name. Although the source code for many languages also use extensions, the .cgi extension is not being used to denote language type, but is a way for the server to identify the file as an executable rather than a graphic file or HTML or text file. Servers are often configured to only try to run those files which have this extension, displaying the contents of all others. Although it might not be necessary to use the .cgi extension, it's still good practice.    (015)

In summary, hello.cgi consists of two main parts:    (016)

Hello, World! in C    (019)

To demonstrate the language-independence of CGI programs, Listing 2.2 contains the equivalent hello.cgi program written in C.    (020)


Listing 2.2. Hello, world! in C    (021)


/* hello.cgi.c - Hello, world CGI */
#include <stdio.h>
int main() {
   printf("Content-Type: text/html\r\n\r\n");
   printf("<html> <head>\n");
   printf("<title>Hello, World!</title>\n");
   printf("</head>\n");
   printf("<body>\n");
   printf("<h1>Hello, World!</h1>\n");
   printf("</body> </html>\n");
}    (022)

NOTE    (023)

Note that the Perl version of hello.cgi uses    (024)


print "Content-Type: text/html\n\n";    (025)

whereas the C version uses    (026)


printf("Content-Type: text/html\r\n\r\n");    (027)

Why does the Perl print statement end with two newlines (\n) while the C printf ends with two carriage returns and newlines (\r\n)?    (028)

Officially, the headers (all the output before the blank line) are supposed to be separated by a carriage return and a newline. Unfortunately, on DOS and Windows machines, Perl will translate the \r as another newline rather than as a carriage return.    (029)

Although omitting the \r's in Perl is technically wrong, it will work on almost all protocols and is also portable across platforms. Hence, in all Perl examples in this book, I use newlines separating the headers rather than carriage returns and newlines.    (030)

A proper solution to this problem is presented in Chapter 4, "Output."    (031)


Neither the Web server nor the browser care which language you use to write your program. Although every language has advantages and disadvantages as a CGI programming language, it is best to use the language with which you are most comfortable. (A more detailed discussion on choosing your programming language is in Chapter 1, "Common Gateway Interface (CGI).")    (032)

<Next | Table of Contents | Previous>    (033)

Copyright © 1997 Sams.Net Publishing    (034)