Super-Charge Your Web Site with CGI Scripts
|
|
|
| 2.3/5.0 (4 votes total) |
|
|
|
Zac Hewlett October 11, 2006
|
A lot of people have web sites but most feel that CGI scripts are "over
their head". Not true! If you know basic HTML and know how to use FTP programs like WS_FTP to upload files, chances are you can super-charge your web site with a CGI script in about 30 minutes.
I'm not talking about writing CGI scripts. All you have to know is how to use CGI scripts. That's the purpose of this article.
With
so many free CGI scripts available on the Internet, you are really
limiting yourself if you are not using the CGI capabilities offered by
your web hosting provider.
With CGI scripts, you can make your
site more interactive with varies functions. You can communicate with
visitors or customers much faster and easier. You can put your online
business onto autopilot.
How does CGI script work?
You've
most probably visited web pages and seen "cgi-bin" appear in the
location bar of your browser. CGI stands for "Common Gateway
Interface". When you see that "cgi-bin" appear in the location bar, you
probably executed a CGI script when you requested that page.
CGI
is utilized by placing an appropriate tag in your HTML code. If you
download free CGI scripts from trustworthy sources on the Internet, the
author of the scripts should provide you with the appropriate HTML tag
needed to run that script. Look for readme file in the downloaded files.
When
the page is requested by a browser, the server reads the tag, locates
and executes the script file that's specified by the tag, and puts in
the tag's place the results of the execution of the script.
If
you've ever looked at your browser's settings, you've probably seen
check boxes or radio buttons to enable or disable Javascript and Java
but haven't seen anything for CGI. That's because Javascript and Java
are executed by the browser. Your browser doesn't know anything about
CGI. It just gets pure HTML from the server after the scripts are
executed.
CGI and Perl
More than often you see the term "Perl" used together with the term "CGI". The two are not the same.
CGI
is the process by which scripts are run. CGI programs, or scripts, can
be written in a variety of computer languages including C. Perl is the
most common language used for writing CGI scripts.
In practice, more than 90% of CGI scripts are written in Perl.
Three Steps To Using A CGI Script
Once
you've downloaded the script you want to use, there are three basic
steps you need to take in order to use the script on your server:
1. Set any options or parameters that the script may need 2. Transfer the script files (the script itself and any necessary data files) to your server and set the correct permissions 3. Add the script's HTML tag to the page(s) you want to use the script
Set Options
"Setting
Options" in scripts is typically just a matter of opening the .pl (or
.cgi) file in a text editor and entering values for some of the scripts
variables. For example, you may need to enter your email address if the
script sends email notifications of some event.
Perl scripts
typically have a .pl extension, but they may have a .cgi extension
instead sometimes. It doesn't really matter. Files with other
extensions, or no extensions, will likely be data files used by the
script. Most script programmers will also include a readme file that
contains information about the script and how to set it up.
More
complex scripts may have more than one .pl file which may require
different HTML tags for each one. It's also possible that one script
may "call" another script so that only one tag is needed. Also, look
for additional .pl files with names like config.pl or cfg.pl. These are
script files where all of the user-settable options are entered and
stored. If a file like this is included in the downloaded file, you
typically don't have to open the main script file to set options. The
main script will refer to this configuration script each time it is
executed.
All of these files are typically combined into a single zipped file.
Some
hosts may require that scripts must have a .cgi extension. It is
normally not a problem to just rename the file to comply. If you do so,
remember to change the extension in the HTML tag also.
Perl
scripts are simply plain ASCII text files. Besides codes for web
server, the programmer can also put comments and instructions in the
script that users can understand. It is easy to spot these information
that is meant for you to read because the line will start with #
character.
The # character is the "comment" character in Perl. Any line that begins with a # does not get executed by the server.
You
can simply use a text editor like Notepad to open the main, or if found
the configuration, .pl file and check the top of the file for any
information or setup instructions. If there is a readme file, open that
in a text editor and look for setup instructions also.
The very
first line of any Perl script is a user-settable option and is always
going to be the path to your server's Perl installation, preceded by
the characters "#!". This line is commonly referred to as the
"shebang". Typical shebangs can be:
* #!/usr/bin/perl (often the Perl 4 location) * #!/usr/local/bin/perl (often the Perl 5 location)
Setting
options usually involves entering values for script variables. These
values can be a '1', 'Y', 'y', 'YES', etc. to enable an option and a
'0', 'N', 'n', 'NO', etc. to disable it. Certain user or system
information may be needed for some variables. You may be asked to enter
path information or an email address.
The comments in the script or the readme file should clearly indicate what the option is and what the valid optional values are.
Transfer Files And Set Permissions
Once
the script is all set up and ready to go, the next step is to transfer
the script files to your web server and set the proper "permissions" to
the files. These permissions are necessary so your web site visitors
can access them properly.
Transferring script files to your web
server is not much different than transferring your web pages there.
More info on transferring files using FTP.
Most web hosts
require CGI scripts to be in the cgi-bin directory. You can create
sub-directories in the cgi-bin for each scripts as well. Some hosts
allow you to place and run CGI scripts anywhere. Check FAQ section of
your hosting service.
If your server is running a UNIX or Linux
operating system, you have to set the permissions to the files. The
UNIX term for setting file permissions is CHMOD.
In your FTP
software, right-click on the script and select "CHMOD" from the pop-up
menu to bring up the Remote File Permissions window. Select the needed
radio buttons according to instructions in the readme file.
In most cases, the .pl files need to be set to "755" while other data files "777".
Add HTML Tags
The
final step is to add the appropriate HTML tag to your web pages to call
the script and then transferring that updated page to the server. The
documentation that came with the script, or the comments in the script
file itself, should include the appropriate tag to use.
You may
have to modify this tag. Most tags assume your CGI sub-directory is
called cgi-bin. If it's called simply cgi, or something else, make the
necessary change in the tag.
Once the tag is added to the appropriate pages, upload them to your server as you normally would.
Congratulations! You've just setup and installed a CGI script. That's all there is to it.
It
may sound complicated while actually it's not. Once you give it a try,
change several lines of codes here and there, you will find it easier
than designing a web page. Much easier and faster I would say. |