Pin Me

Simple Guide to Writing CGI Scripts

written by: Brandy A Burgess•edited by: Simon Hill•updated: 3/28/2010

Would you like to create interactive elements such as shopping carts and page counters on your webpage? When you learn how to write CGI scripts, you can use this knowledge to spice up your webpage.

  • slide 1 of 6

    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.


  • slide 2 of 6

    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.

  • slide 3 of 6

    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: <HTML> Press 'Enter' <HEAD>Title Goes Here</HEAD> Press 'Enter' <BODY>Body Content Goes Here</BODY>Press 'Enter'</HTML>. Hit the ‘Enter’ key again to close the script with the EndofHTML tag.

  • slide 4 of 6

    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.

  • slide 5 of 6

    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.

  • slide 6 of 6

    Example of a Basic CGI Script


    print <<EndOfHTML;



    <HEAD>Title Goes Here</HEAD>

    <BODY>Body Content Goes Here</BODY>