CGIScriptDepot.com
Your source for CGI Scripts, Tutorials, Remotely Hosted Scripts and more...

Home | CGI Scripts | 10 Newest Files | 10 Top Downloads | Suggest a Script |
CGI Script Resources
CGI Script Listings
Newest CGI Scripts
Most Popular CGI Scripts
CGI Script Code Bits
CGI Script Tutorials
Link Partners
 
 
Related Links
Web Hosting
Domain Names

What is a CGI Script?

CGI or “Common Gateway Interface” is basically a standard that allows web users to run programs on a web server, and view the results from their browser. It is not some programming language; rather it sets the rules that allow programs or scripts written in other languages to be run over the Internet.

To understand the concept in a better way, let’s have a look at what happens at the user end. When you type a URL, your web browser communicates with the web server using the HTTP Protocol. The web server uses the URL requested, and the data passed in the HTTP Protocol request to determine how it responds to the request.

If the URL requested is for a simple file, the web server searches for the file and sends it back to the browser. Ultimately, the file is parsed and then displayed in your browser in an apposite format.

If however, the URL you're requesting is for a script, the web server will run the script on the server and return the results to the browser. This interface that the web server uses to communicate with the script is CGI. Therefore, most programs that you write to handle the input-output from web server are known as CGI scripts. CGI scripts are typically, but not always stored in the cgi-bin folder on a web server.

What can I use CGI Scripts for?

There are several reasons for preferring CGI to the other methodologies.

If you want to include some interactive feature in your web site, like visitor counters, opinion polls, discussion boards, etc - you will need to use some kind of CGI script running on the web server. CGI Scripts give you tremendous power and flexibility to interact with users, because your scripts can respond to users based on data, calculations or other programatic criteria.

Another technique for interactive web sites is to use Java Script. Java Script is a browser-based scripting language, that is it is interpreted and run in the user’s browser. It does not run on the server at all, Java Script code is simply delivered to the user like any file.

Minimum requirements for running CGI Scripts

The first requirement for running a CGI Script is the proper file system location. Most web hosts provide access to a special cgi-bin folder that has permissions to run CGI Scripts. Some hosts allow you to run a CGI script anywhere in your web hosting account, as long as it has the right extension. (We recommend and use AssortedInternet.com as our web host.)

Two other important details for CGI Scripts are the file extension and the file permissions. The proper extension to use for a CGI Script is typically ".cgi" or ".pl". All CGI Scripts should have their permissions set to 755. To set the permissions properly you can use your FTP program or run the following command in a UNIX shell session:

  • chmod 755 filename.cgi

Editing CGI Scripts

CGI Scripts are like normal text files with extension .pl or .cgi. Therefore, you can open and edit CGI Scripts using your favorite text editor. We don't recommend using Notepad. You can find much better! Try UltraEdit-32 if you don't already have a favorite text editor. UltraEdit-32 supports syntax highlighting, formatting and other handy features.

Basic Parts of a CGI Script

Every CGI Script has a few basic parts in it. First, you is to determine the path to your Perl Compiler. The Perl compiler will be specified on the first line of your script. Generally the Perl path is:

  • /usr/bin/perl

But this is not always true. If you don't know the path to perl on your system, ask your web host or system administrator. Or, if you have access to the UNIX shell, run the following command:

  • which perl

to find the location of Perl on your server. Once you have that, you have the first line of your CGI Script. The first line is:

  • #!/usr/bin/perl

This first line of your CGI Script tells the server that you're script is a Perl script and how to run the script on the server.

Once you know the path to Perl, you'll need to know the file system path to the cgi-bin location of your site. It would look something like this:

  • /home/myname/public_html/cgi-bin

If you don't know the file system path for your web site on your system, ask your web host or system administrator. Or, if you have access to the UNIX shell, run the following command after you login:

  • pwd

Typically you'll get a response like:

  • /home/myname

that should help you determine your actual file system path for your web site.

Now What?

Now that you know the location of Perl and of your web site on the file system, you can create and upload a basic CGI Script. So, lets create your 1st script. Copy the text below and save it as a text file named "hello.cgi". #!/usr/bin/perl print "Content-Type: text/html \n\n"; print "Hello World!\n\n"; exit();

Now use your favorite FTP program and upload the script to your web server. Set the permissions to 755, and try to access it in your browser. It should respond back with: Hello World!

Congratulations on creating your first CGI Script!

Learn More

We maintain a handy list of Perl and CGI Scripting Tutorials that can help get you started creating your own CGI Scripts. Even ifyou are already writing your own CGI Scripts- you might learn a thing or two in our more advanced tutorials.

CGI Script Tutorials