How to Write CGI Scripts
- 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.