How to Write CGI Scripts

How to Write CGI Scripts

CGI (Common Gateway Interface) is not necessarily a programming language, but more of an intermediary function in website design. To use CGI, you should be experienced with HTML and have a working knowledge of PERL. The reason you need to be experienced in HTML is that CGI scripts only collect data and send it to the server. In other words, you will create HTML files for collecting that data, and the CGI script prints out that data according to instructions.

Instructions

Writing a CGI Script

    1

    Download Notepad++. The reason for this is that Notepad is a good way to learn and memorize coding and programming rather than having programs such as Dreamweaver do the work for you. Once you are experienced, using Dreamweaver (or a similar program) can be faster. However, if you do not understand what is being produced, you would not know where to look for the problems. So, it is advised to start with "hand writing" your code. Notepad++ takes Notepad further and gives it capabilities that help programmers and web designers. In this program, you will be able to choose what language you are writing in, and different sections of code are colorized to represent proper format.

    2

    Open Notepad++ and type: !/usr/local/bin/perl. This is the first line to every CGI/PERL script you will write. The hashmark () denotes a comment. You will use comments often in your larger scripts to keep track of what is happening. /usr/local/bin/perl is the path to your CGI/PERL functions. If you are using a server online, then ensure that this path is correct with your host. It is a default path, so 9.9 times out of 10 this can be left alone.

    3

    Hit enter twice so that there is a blank line in your code. Then type: print <
    or

    in HTML. However, the line of code you should have copied was not the very basic function. Notice < 4

    Hit enter once and type: Content-type: text/html. This is self-explanatory. It is telling you what type of content the CGI script is going to produce. In HTML, you have document type at the top of your file. This is the same thing. At this point you can relax ... you are going to go back to good old fashion HTML.

    5

    Remember that you must have a blank line between your header and functions. So, again, hit enter twice. Then type:My First CGI Script

    Simple CGI

    Through the use of HTML, this CGI script will print whatever I type here.

    If you've read the instructions above about needing to have experience with HTML, then none of that should have to be explained.

    6

    Hit enter one more time and type: EndofHTML. You know what that is, right? I thought so. It is the closing tag to those multiple lines of HTML, and now the CGI script knows that it can print everything you have typed there. Yes, this does include styling, headers and anything you can do in HTML.

    7

    Save your file as mycgi.cgi.

    8

    Upload your CGI script to your web server. You may put it in your cgi-bin, but it is not necessary. When you become more experienced with CGI and develop complex scripts, it will be necessary to put those scripts in the cgi-bin. So, getting in practice of doing so is beneficial. If you do not have a web server to upload to, then a local server will work. Follow the link to the XAMPP homepage and install XAMPP on your computer as a local server for testing scripts. This is a key program for many web designers and programmers.

    9

    Call your CGI script through your browser by going to: http://yourwebhost/mycgi.cgi or - http://localhost/mycgi.cgi (if using a local server).

    10

    Understand that it is beyond the scope of this guide to give you complete and detailed instructions on how to write CGI scripts. Many resources on the Internet have tutorials on this subject. Searching Google with keywords relating to exactly what you wish to learn is preferable. Almost all CGI scripts will follow this basic format. Advanced education in CGI will allow you to insert other functions, such as variables and arrays, which will allow you to use CGI to collect information from form submissions and print them to a server or email them.

Blog Archive