How to Write CGI Scripts
Page content

CGI Scripts

With the many benefits that CGI offers, it’s only natural that many new programmers want to learn how to write CGI scripts. CGI, or Common Gateway Interface, is a basic protocol that is commonly used to communicate between a program and web forms. Although not a language itself, CGI scripts can be written in virtually any programming language.

.

Getting Started

Before you can begin writing CGI scripts, you will need to download a program known as ‘Notepad ++’. Beginner developers should consider using a basic program such as Notepad ++ as it allows you to memorize the programming language. More experienced web developers may prefer enhanced programs such as ‘Dreamweaver’ that do most of the coding for you.

Developing Your Code

Open ‘Notepad ++’ on your screen and on the first line of your script, type in the following: #!/usr/local/bin/perl. This segment of code will represent the first line of every CGI/PERL script. The ‘#’ character in the front of your script represents comments that will be used often in larger scripts. The remainder of the script /usr/local/bin/perl, represents the path to CGI/PERL functions. The default function should be used.

Hit the ‘Enter’ key twice to create a blank line in the script and type the following: print «EndOfHTML;. This segment of code represents the primary function and will print whatever you type into this section. The closing tag is EndofHTML. Although there are other ways to close a tag, it’s important to keep both the beginning and end tags the same. Re-read your script thus far and check for spelling mistakes or other errors.

Begin a fresh line on the program by hitting the ‘Enter’ key once. Type the following into your script: Content-type: text/html. This segment of script will tell you the content of your CGI script. Continue by hitting the ‘Enter’ key twice to create another blank line between the functions and header. Type the following into your script: Press ‘Enter’ Title Goes Here Press ‘Enter’ Body Content Goes HerePress ‘Enter’. Hit the ‘Enter’ key again to close the script with the EndofHTML tag.

Using Your CGI Script

Save your final CGI script to a file name that you will recognize. Next you will need to upload your script to a web server. If you currently do not have a web server, you can also opt for a local server. It’s important to test your script by following the XAMPP homepage and installing it onto your computer. XAMPP is a user-friendly Apache distrubution web server package which includes Perl, PHP and MySQL programming languages. This easy to use web server requires only one exe, zip or tar file to be downloaded and little or no configuration is needed.

Further Reading

For more advanced education in learning how to write CGI scripts, the Internet provides many useful and detailed tutorials for a variety of programming languages for beginners and expert web developers. When you learn how to write CGI scripts, you can go on to use your new found knowledge to create shopping carts, guest books, page counters and other elements to your webpage.

Example of a Basic CGI Script

#!/usr/local/bin/perl

print «EndOfHTML;

text/html

Title Goes Here Body Content Goes Here

EndofHTML