CGI Scripts
CGI Scripts are generally written in a computer programming language called PERL which is available on virtually all Unix and Linux servers. PERL is slightly different than other programming languages like C in that it is not compiled. A PERL Script will use the actual PERL Language on your server to execute the script in real time while the page loads on your users screen, and as they interact with it via buttons and links. C and other languages are compiled and self contained and you can not edit them, get into them and look around or anything else. A CGI Script is editable with any good Word Processor (like NoteTab) and you can customize them for your web site. Many scripts are easily cutomized for the look and feel of your site, so it seems like it was made just for you!
CGI Scripts are what make the Internet interactive. Scripts are behind what makes the internet tick. There are thousands and thousands of CGI Scripts available for your use, many for free, all over the Internet. CGI Resources is an excellent site to get them from. They have a huge variety of scripts. Some you pay for, some you get for free.
All CGI Scripts have one thing in common, that is the way they start off. They start off with what is called the bang line which tells the server it's a script and the location of PERL on the server for the script to use. This absolutely MUST be the very first line of the file.
#!/usr/bin/perl
#!/usr/local/bin/perl
Those are two example of the bang line. They are the path of PERL on your server. On my dedicated server either way works. That doesn't mean it will on your site though. One or the other should do the trick. If you can log in via telnet, you can find the path to PERL by typing which perl. But for the sake of this HTML Tutor we are just touching on the subject of PERL CGI Scripts and will give you a quick overview of them
Many scripts will have a configuration file, or at least several lines that need to be configured. This isn't as difficult as it sounds. I'll show you a simple configuration and you can mess around with scripts on your own, with just this little bit of knowledge.
Here is part of a configuration file which is from a script that I bought some years ago. The script builds a database of links that get categorized by subject matter. It's a great script and works flawlessly. The script consists of many files, each having a special unique function.
In an actual implementation the below code would all be put together in a file of it's own. That is the usual case although sometimes the configuration data is placed at the beginning of the main script. Here I'm going to pick the file apart and show you the pieces, with a short explanation.
Inside of a cgi file is a lot of code. The author, or at least good authors, will place comments within the script. These comments, basically notes to and from the author, tell about what is happening with the script. Comments begin with a pound sign or bang (#). Everything to the right of the bang is ignored by the script when it executes and serves only as information to the human who is reading the script.
# PATH and URL of Admin CGI directory. No Trailing Slash.
$db_script_path = "/home/www/yourdomain/cgi-bin/scriptname/admin";
$db_dir_url = "http://www.yourdomain.com/cgi-bin/scriptname/admin";
The above defines the path to the script Admin directory, and then the URL to that directory. The piece of code $db_script_path is used over and over all throughout the script when administration functions are used.
# URL of User CGI directory. No Trailing Slash.
$db_cgi_url = "http://www.yourdomain.com/cgi-bin/scriptname";
Like the above script path the $db_cgi_url defines the path to the parts of the script that users would run, when, in this case, they add their URL to the database.
# PATH and URL of Pages to be built. No Trailing Slash.
$build_root_path = "/home/yourdomain/www";
$build_root_url = "http://www.yourdomain.com";
The information after the pound sign pretty much tells you what is going on and what information is needed.
# PATH to sendmail.
$db_mail_path = '/usr/bin/sendmail';
There is the path to sendmail defined. Whenever the script is required to email results and information somewhere it calls this variable up so that it can find the sendmail program and send the email. All of these variable can be found out from your web host.
# Email address of the administrator. BE SURE TO SET!
$db_admin_email = 'administrator@yourdomain.com';
Here is where you define where you want your email to be sent.
# Log all outgoing messages? Put the full path to a logfile and make sure it
# exists and is writeable (chmod 666 -rw-rw-rw-). This logfile can get pretty
# big!
$db_mailer_log = '/home/yourdomain/www/outgoinglog/log.txt';
This particular script lets you log all the emailing activity that the script performs. This variable defines the location and name of the log file to use.
# Site Title, the default title to use. You should really spend some time customizing
# the directory though!
$build_site_title = 'The Name of My Web Site';
As the note says, this is the name of your site. It would be used when the script builds a web page and the script would insert that name into the <title> tag. That should be enough to give you an idea of what a configuration file is.
There are quite a few things happening in this configuration file. The location of PERL is not included in a configuration file because this file is not being executed when the script runs, it's just an informational file the script uses to find things, and is where all of the information it needs to run is found. It's sort of like the telephone book at your house. You never really sit down to read it, but you refer to it for information.
Beyond this, CGI Scripts get much more complicated and is beyond the scope of this HTML Tutor. I just want to touch on them so you know what they are, and explain a little about them so you can try them out. It's not as hard as it looks to figure out how to configure a cgi script, but you must be very careful because omitting one comma, or colon or whatever will break the script and it won't work. Figuring out what is wrong can be next to impossible.
AXS is a tracking script. It tracks where your visitors have come from, along with a lot of other information. It is very useful, and is fairly simple to setup. The instructions that come with it are very good. It will help you with marketing too, becuase it shows how people found your site, from which search engine or other sites link they came from, and what search term they used to find you. Invaluable information. The script is free from Fluid Dynamics and you can get it at this link.
I have included a counter script and an inclusion script for you on the next page. I suggest you copy and paste them onto your computer in a directory called cgi-bin. Save the files as counter.cgi and footer.cgi. They are both very simple scripts and are easy to setup. I think it's a good starting point for you to begin using scripts on your web pages.
When you upload it to your server, upload it in ACSII to the cgi-bin and then do a change mode 755 to the file. Using WS-FTP highlight the file on your server and right click your mouse, then select Properties from the drop down list. Change the number (probably 644) that you see in the little box to 755. Some programs require you to actually click the boxes next to all those read, write and execute lines. If yours is like that you need to click Read, Write and Execute in the Owner column, and then Read and Execute in both the Group andWorld columns. Confusing, yes. I know. Just do as I said and it will work OK. You'll find some sense to it some day after you've played around with scripts and FTP uploading for a while. What that does it change the script's permissions to execute and run the program on your server.
The counter file is here
To use the counter put this line of code on your page where you want it to show the count.
<!--#exec cgi="counter.cgi"-->
Upload your page and when you refresh you'll find that you have a number where you put the above piece of code. Each time you get a visitor it will increment. It's nice to see it climb!
You can do a search for fresh sites offering CGI scripts. One very good resource is CGI Resources. There are lots more. Do a search for them. There is a ton of free and low-cost software around for the Unix/Linux web server. There is a lot of expensive software too, if you get into things and can afford to spend.
<
Previous: CSS Background Properties
—
Next: More CGI Scripts >
|