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.

Things You'll Need

  • Knowledge of HTML
  • Knowledge of Perl
  • Web server (online or local)
  • Editor (Notepad++ is an alternative to using Microsoft's Notepad)
Show More

Instructions

  1. 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 <<EndOfHTML;. This is the primary function of a CGI script. It prints what you tell it to print. The very basics of this function is: print "Hello, world!\n";. This would print "Hello World" to the web server when called. The \n inserts a newline or blank line afterwards. It is the same as using <br><br> or <p> in HTML. However, the line of code you should have copied was not the very basic function. Notice <<EndofHTML;. What this does is allow for multiple lines of code to be inserted and the print function will wait for the "closing tag" and bring everything in between. The closing tag, of course, is 'EndofHTML'. Keep in mind that it does not have to be that. Your tag could simply be HTML or anything you choose, as long as you begin and end the section with the same tag. The CGI script will wait to see your tag and print everything in between, so it is imperative that you double check spelling and letter casing.

    • 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:<HTML><HEAD><TITLE>My First CGI Script</TITLE></HEAD><BODY><H1>Simple CGI</H1><P>Through the use of HTML, this CGI script will print whatever I type here.</P></BODY></HTML>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.

Tips & Warnings

  • If you have installed XAMPP as a local server, keep in mind that your first line is going to look similar to this: #!"C:\xampp\perl\bin\perl.exe".

  • If you are testing your script on a local server and then uploading to an active server, remember that you will need to change the path-to-cgi (the first line in the script).

Related Searches:

Resources

Comments

  • diginb Sep 29, 2008
    excellent tips...thanks!
  • diginb Sep 29, 2008
    excellent tips...thanks!

You May Also Like

  • How to Write a Short Film Script

    So you want to be the next Robert Holmes and write a script for a short film? First you need a good...

  • How to Write an Animation Script

    Scripts for animation movies and TV shows are different from ANIMEE scripts, which have their own rules. They are very much live...

  • How to Upload CGI Files

    Common gateway interface, or CGI, files require specific file permissions when they are uploaded to the Web . You can check for...

  • How to Write HTML Forms

    Start the form with form > and end the form with the /form > tags. These tags define the content of the...

  • How to Write HTML Script

    With the hypertext markup language (HTML) "script" tags, you can enhance your Web pages with a client-side script like JavaScript or VBScript....

  • How to Find Local NTP Servers

    Network Time Protocol (NTP) servers are used to keep computers across the world synchronized. Servers are usually based on the most accurate...

  • How to Use CGI Proxy

    CGI is an older method of writing scripts for the Internet . Although it has been pushed aside as more advanced scripting...

  • How to Use a Unix Shell Script to Create an HTML Web Page

    Unix shell scripts can be used to create web pages that display the output of basic Unix commands or complex programs. This...

Related Ads

Featured