Pin Me

CGI Tutorial: An Example CGI Script

written by: Ossy Uche•edited by: Simon Hill•updated: 4/1/2010

Have you ever wanted to add some interactivity to your site with guestbook forms, shopping carts, counters or other dynamic elements? If so, then check out this CGI tutorial and example of a CGI script.

  • slide 1 of 2

    What is CGI?

    If you are a web designer, a student, a teacher, an entrepreneur, or anyone that owns a website and you want to add some interactivity to your site by adding guestbook forms, shopping carts, counters or other dynamic elements, then you should be interested in learning CGI. CGI is easy to learn really, and there are a lot of CGI tutorials that can help. If you know a little HTML, then you can write CGI. So what is CGI?

    CGI means Common Gateway Interface, which is a method that allows web users to run programs from their computer. By doing this, a web server can receive data from or send data to databases, documents, and other programs, and finally deliver the data to viewers via the web. CGI by itself is not a programming language, but it’s a process that enables scripts or programs written in other languages to be run.

    CGI scripts or programs usually receive data given to them from a form on a web page, then process the data and finally format and output the results as an HTML document. The resulting web page is a dynamic web page. The most acceptable programming language for writing and processing CGI is Perl, or “Practical Extraction and Reporting Language". This was initially developed to handle multiple text files and format them nicely, but is more widely used to write CGI alongside HTML now.

  • slide 2 of 2

    Example of a CGI Script

    In the picture below you will see an example of a simple CGI script written with Perl Language. Let us now explain what it’s all about in the CGI tutorial below.

    cgi-tutorials  1. Line 1 shows the path to the perl compiler or processor in your system. This varies depending on your web server’s configurations. You may need to ask your web hosting provider or administrator for the correct syntax if you are not sure. The usual alternative location on Unix servers is #/usr/local/bin/perl, while the usual location for a windows server is #!c:/perl/bin/perl.exe. You may need to put this line at the top of your CGI scripts on a windows machine depending on your system configuration. Then the – w is for warnings, which you should always have in your scripts to ensure your codes are free of bugs.

      2. it’s an empty line that should always come after the call to the perl interpreter.

      3.This enables your script to specify variables early and follow other appropriate programming conventions. Applying this equally reduces code error as well. However it is important to note that after every line with a perl command such as echo, you have to end it with a semicolon.

      4. This line is also very important in a CGI Code as it helps in debugging the script. It will give you a better description of the error instead of the common “500 Internal Server Error".

      5. This line inputs the module which includes pre-specified functions which are vital to CGI programming.

      6. This is a function which outputs the HTTP Content-type header, a major requirement in all CGI programs. It’s good practice to have this line near the top of all of your CGI scripts, so that you will not forget to put it in.

      7.This uses perl’s in built print functions to dynamically output a string of text in the browser screen for you. You can use either double or single quotes for this.

      Finally with a little research there are a lot of places you can easily get free CGI tutorials online as well as free CGI scripts already pre-written for immediate use on your website. So go ahead and add interactivity to your websites as you wish.