|
||||||||||||||||||||||||||||||||||||||||||
|
CHAPTER THREE - Cgi-bin scripts3.1) OVERVIEW
3.2) FEEDBACK FORMS, GUESTBOOK & SEARCH
3.3) IMAGEMAPS3.4) HTML PAGE COUNTERS
3.5) READY TO RUN SCRIPTS & ALL SITE COUNTERS
3.6) TROUBLESHOOTING3.1) OVERVIEW3.1.1) What are cgi-bin scripts?"cgi" stands for "common gateway interface", a fancy name meaning computer programs running on the web server that can be invoked from a WWW page at the browser. The "bin" part alludes to the binary executables that result from compiled or assembled programs. It is a bit misleading because cgi's can also be Unix shell scripts or interpreted languages like Perl. A typical use for cgi is the processing of online forms. When the user fills in the boxes on the form and hits the SUBMIT button, the cgi program specified in the html will be run at the server, and the information in the boxes become available to the program as parameters. The program, being a program, can then do anything the programmer wanted it to do. "cgiemail", for example, is a canned program written in C that gathers up the contents of the boxes on the form and emails them to a specified destination, then sends a WWW page confirming the action. 3.1.2) Where should I put cgi-bin scripts?Put your cgi-bin scripts in the WWW subdirectory named cgi-bin. If your domain is named company.com, you would then access your scripts as /cgi-company/ 3.1.3) What is the path of date, mail, perl ...?To find the path of the program date, type which date. Do this for any program that you need your cgi-bin script to access. I see lots of perls, which one should I use? They should all be the same. /usr/local/bin/perl should work nicely. 3.1.4) Custom ScriptingNexus Internet Solutions can design and program scripts for you. Please contact us at Websites@nexus-iservices.com for more information. Your Linux control panel maintains a set of free CGI scripts for more common tasks 3.2) FEEDBACK FORMS AND SIMILAR FORMSCgiemail is a program written in the C language that takes the contents of fill-in boxes on a form and emails them to a specified destination. In addition to the form specification in the .html file, a mail specification in a .txt file is required to format the resulting email message. We provide cgiemail in the cgi-bin directory of each machine. Thus, it would be called with a URI such as /cgi-bin/cgiemail/mail.txt. Details are provided below. While there are a number of subsections below this one, they all work together and are meant to be read from start to finish. 3.2.1) add.htmlIf you've never dealt with HTML forms before, don't worry. They're easy to create and understand. (And if you briefly tried that link and thought, noooooo!!! I don't want to read this!, scroll down a bit. The easy examples are at the bottom. You should not need to follow it at all to understand what will be explained presently.) The form prompts the user for data which is sent to the server as simple key-value pairs. Each <input> tag specifies a record. The key is given by the name attribute, and the value is given by the value attribute. The type attribute tells the browser what kind of data to expect. Now, try looking at the example. Please note that the hidden items are used to transmit critical info to cgiemail. They provide the location of the success file, the name of the person the results should be sent to, and the subject of the form. When making your own forms, you would definitely (please!) change the e-mail address in the "required-to" field, and likely the subject in the "subject" field. The first item tells cgiemail what to show the user after succesfully completing the form. You can, but don't need to, customize this. After that come the items that are actually presented to the user. You'll want to use type=text input items with cgiemail; it's a simple tool. The size=60 tells the browser how big to make the box. The name=something is required in each input tag, otherwise the browser wouldn't know how to send the data to the server. The value="" attribute is correct in most cases, unless you want a default value in the form. Note that if a field begins with required-, cgiemail will require that the user enter a value for this field. This is particularly useful if you want to require a user to submit their e-mail address. When the user presses the Submit button, the data goes to our machine where cgiemail starts doing something with it. What it does is controlled by ... 3.2.2) Mail.txtMake sure that you upload mail.txt in ASCII mode. Failure to upload mail.txt in ASCII mode will generate the message: Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Now that we have all this data, what do we do with it? Mail it, of course! But for flexibility, cgiemail requires that you create a mail.txt file to show it what to send. (If you didn't want flexibility you'd use a mailto link.) The program will read in this file, perform substitutions, and pass it to the mail system. First, how does cgiemail know where to find this file? Look at the first line in the example form used in the previous section. The bit after the word cgiemail in the action attribute tells cgiemail where to look. When called from yourdomain.com, cgiemail does not know what your domain is because it is installed for everyone. So, the first component of this path must be your domain. After that, it goes to your www directory and looks from there. If you have a domain name, we suggest that you explicitly put it in the action attribute, because the behavior is different: cgiemail does know what your domain is if called for your domain. Fred.com might want to put the mail.txt file in his www directory. His form would then start off with <form method=post action="http://www.fred.com/cgi-bin/cgiemail/mail.txt"> BTW, there's nothing magical about the name mail.txt. Feel free to call it mail1.txt, or form1.mail, or what suits you, as long as the form has the correct name for what you uploaded. Here's mail.txt we used with the form presented earlier. It's pretty simple. Note that the first several lines are mail headers. You probably shouldn't change that part, or the corresponding parts in your form. In particular, there must be a To: header or the mail won't go anywhere! What cgiemail does is simply replace every string that looks like [key] with the value the user typed in to the field with name=key. That's all. You can lay out your form as is best for your users, but lay out your mail.txt as is best for you to read. You can even insert gobs of text to help format the output. Only the [key] parts will be replaced by cgiemail. 3.2.3) How to use cgiemailActually, if you've read the previous two sections you should know enough to use it. We'll summarize here.
Cgiemail was developed at MIT and is copyright 1994, 1995. Formmail.cgi Formmail is a program written in the Perl language that takes the contents of fill-in boxes on a form and emails them to a specified destination. In addition it is also capable of generating its own Confirmation page. Setting Up the FormMail Script: The FormMail.pl script does not have to be extensively configured to get it to work. There are only two variables in the perl file which you will need to define along with changing the top line of your script to match the location of you Perl interpreter. Necessary Form Fields: There is only one form field that you must have in your form, for FormMail to work correctly. This is the recipient field. Field: recipient Description: This form field allows you to specify to whom you wish for your form results to be mailed. Most likely you will want to configure this option as a hidden form field with a value equal to that of your e-mail address. Syntax: <input type=hidden name="recipient"value="email@your.host.xxx"> ----------------------------------------------------------------------------- Optional Form Fields: Description: The subject field will allow you to specify
the subject that you wish to appear in the e-mail that is sent to you after this form has
been filled out. If you do not have this option turned on, then the script will default to
a message subject: WWW Form Submission Syntax: If you wish to choose what the subject is: To allow the user to choose a subject: ----------------------------------------------------------------------------- Field: email Description: This form field will allow the user to
specify their return e-mail address. If you want to be able to return e-mail to your user,
I strongly suggest that you include this form field and allow them to fill it in. This
will be put into the From: field of the message you receive. Syntax: <input type=text name="email"> ----------------------------------------------------------------------------- Field: realname Description: The realname form field will allow the user
to input their real name. This field is useful for identification purposes and will also
be put into the From: line of your message header. Syntax: <input type=text name="realname"> ----------------------------------------------------------------------------- Field: redirect Description: If you wish to redirect the user to a
different URL, rather than having them see the default response to the fill-out form, you
can use this hidden variable to send them to a pre-made HTML page. Syntax: To choose the URL they will end up at: To allow them to specify a URL they wish to travel to once
the form is filled out: <input type=text name="redirect"> ----------------------------------------------------------------------------- Field: required Version Added: 1.3 Description: You can now require for certain fields in
your form to be filled in before the user can successfully submit the form. Simply place
all field names that you want to be mandatory into this field. If the required fields are
not filled in, the user will be notified of what they need to fill in, and a link back to
the form they just submitted will be provided. Syntax: If you want to require that they fill in the email and
phone fields in your form, so that you can reach them once you have received the mail, use
a syntax like: Field: env_report Version Added: 1.3 Description: Allows you to have Environment variables
included in the e-mail message you receive after a user has filled out your form. Useful
if you wish to know what browser they were using, what domain they were coming from or any
other attributes associated with environment variables. The following is a short list of
valid environment variables that might be useful: REMOTE_HOST - Sends the hostname making a request.
REMOTE_ADDR - Sends the IP address of the remote host making the request.
REMOTE_USER - If server supports authentication and script is
protected, this is the username they have authenticated as. *This is not usually set.* There are others, but these are a few of the most useful. Syntax: If you wanted to find the remote host and browser sending the request, you would put the
following into your form: ----------------------------------------------------------------------------- Field: sort Version Added: 1.4 Description: This field allows you to choose the order in
which you wish for your variables to appear in the e-mail that FormMail generates. You can
choose to have the field sorted alphabetically or specify a set order in which you want
the fields to appear in your mail message. By leaving this field out, the order will
simply default to the order in which the browsers sends the information to the script
(which isn't always the exact same order they appeared in the form.) When sorting by a set
order of fields, you should include the phrase "order:" as the first part of
your value for the sort field, and then follow that with the field names you want to be
listed in the e-mail message, separated by commas. Syntax: To sort alphabetically: To sort by a set field order: ----------------------------------------------------------------------------- Field: print_config Version Added: 1.5 Description: print_config allows you to specify which of
the config variables you would like to have printed in your e-mail message. By deafult, no
config fields are printed to your e-mail. This is because the important form fields, like
email, subject, etc... are included in the header of the message. However some users have
asked for this option so they can have these fields printed in the body of the message.
The config fields that you wish to have printed shoul dbe in the value attribute of your
input tag separated by commas. Syntax: If you want to print the email and subject fields in the body of your message, you would place
the following form tag: ---------------------------------------------------------------------------- Field: title Version Added: 1.3 Description: This form field allows you to specify the
title and header that will appear on the resulting page if you do not specify a redirect URL. Syntax: If you wanted a title of 'Feedback Form Results': ----------------------------------------------------------------------------- Field: return_link_url Version Added: 1.3 Description: This field allows you to specify a URL that
will appear, as return_link_title, on the following report page. This field will not be
used if you have the redirect field set, but it is useful if you allow the user to receive
the report on the following page, but want to offer them a way to get back to your main page. Syntax: <input type=hidden name="return_link_url"value="http://your.host.xxx/main.html"> ----------------------------------------------------------------------------- Field: return_link_title Version Added: 1.3 Description: This is the title that will be used to link the user back to the page you
specify with return_link_url. The two fields will be shown on the resulting form page as:
Syntax: <input type=hidden name="return_link_title"value="Back to Main Page"> ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- If you have an account that includes a preconfigured guestbook, You simply need to use
the following URL to access it. http://yourdomain.com/Guestbook/guestbook.html 3.2.5) How to use Search.cgi http://yourdomain.com/cgi-yourdomain/search.cgi 'Imagemap' refers to an interaction between a user's browser and the WWW server which
allows clicking on an image to select hyperlinks. (Other implementations have a seperate
/cgi-bin/imagemap program. Our server handles them internally. Use the reference style
shown below, not one like /cgi-bin/imagemap/mymap.map.) Each designated portion of the
image may select different links. Images that are to be used as imagemaps must be designated
as "ISMAP". A companion .map file specifies the coordinates of the portions of
the image that are hyperlinks, and the URL or local reference of the link. The following HTML:
<A HREF="name-of-map-spec-file.map">
<IMG BORDER SRC="name-of-image.gif" ISMAP></A> tell the browser
that "name-of-image.gif" is to be displayed and used as an imagemap. The
"name-of-map-spec-file.map" file contains the coordinates of the clickable
areas in the image and specify the hyperlinks associated with each clickable area. The map specification file MUST have extension
".map". While this example assumes the file is located in your www directory, it
may be located anywhere under the www directory. In such a case, the path from the www
directory must be specified. If you put it in /home/your-id/www/map-directory, then use
<A HREF="../map-directory/name-of-map-spec-file.map"> Please use the NCSA format
for your .map file. A file named imagemap.conf does NOT have to edited when
using our imagemap program. That complication has been removed from our version of
imagemap. Here is a sample file named fo-abc.map:
Note that the URL's are full URLs. You can't use relative URLs. The number pairs specify the X-Y coordinates in pixels of
the points of the three hexagons that are the clickable areas in fo-abc.map. Clicks
outside of the areas specified get the default link. index.cgi, will be found in your cgi-bin. which you will use to update a counter on
your home page. Put this file in your main web directory, you don't need to put it in a
special cgi-bin directory. It must, however, be called index.cgi to have the desired
effect. Type chmod +x index.cgi to activate index.cgi. Then create a file named .count with one
line that contains the number 0. Then type chmod o+w .count to allow the counter to be
updated. (chmod 666 .count if you're stuck with FTP.) The counter and index.cgi are
in your main page in this example. When http://your-domain.com is referenced at the browser,
instead of delivering the file index.html as normally expected, the program index.cgi is
run instead. index.cgi reads the .count file, increments the number in that file by one,
and writes the file back on disk. Then it reads in the index.html file, finds the string
"#COUNT" imbedded somewhere in it, changes "#COUNT" to 1 + whatever
value if found in .count, and sends the modified index.html to the browser. To create a counter on a page other than your home page,
change the source from index.html to the page that you wish to update. You would want to
make another copy of index.cgi as well. So for example, if you wished to count a page
called orders.html, you would change the source from index.html to orders.html, change the
name from index.cgi to orders.cgi and then call this page from other pages by the name
orders.cgi. Change two things within orders.cgi: find the string 'index.html' and change
it to 'orders.html', and find the string '.count' and change it to '.countorders'. Both
are right near the top of the program. Then make a file .countorders instead of .count,
but otherwise as directed above. Very simple to use, first the page which you would like to count must have the
extension .shtml or .sht rather than .htm or .html The World Wide web Board is already to run, you can call it from a page with the
following URL: http://yourdomain.com/bbs/ Placed in your WWW directory, you will find a sub directory called secure. This has
been setup with the password file in place, and it cannot be entered from the web without
first being prompted for a login and password. (This section will grow as we remember to add what we keep doing every day anyway.) "When I activate my CGI program, I get back a page that says 'Server Misconfigured'". Log on and go to your www/cgi-bin directory. Try
running the program manually to see what errors you get. Remember that when a CGI program
produces error output, that output is discarded and you get the non-specific page you saw.
To check on it, you'll have to run it yourself. If you get something like bash: ./search.pl: No such file or directory and you know
you typed the name correctly, it's almost certain you uploaded the script from your PC or
Macintosh in binary mode. Perl scripts are ASCII text and must be transferred in ASCII
mode. (So are shell scripts and any other kind of script that starts with '#!' on the
first line.) You know it's this if ./search.pl doesn't work and perl search.pl does.
Assuming you don't have any legitimate carriage returns in the program (doubtful), this
will fix it: mv search.pl search.pl.txt && tr -d '\r' Then there's this response: Literal EXOHost
now requires backslash at ./bigones line 16, within string Many scripts that you'll find widely distributed are still written
for Perl, version 3. Our /usr/bin/perl is version 5, which is 99.9% compatible with
version 4, along with many improvements. You've found the other 0.1%. In Perl 4, you could get away with a statement like $mailaddress ="joe@schmoe.com"; because Perl didn't think
the '@' sign was special in a string. Perl 5 does, so you need to escape it, like so:
$mailaddres ="joe\@schmoe.com"; You can't just change all
the '@' signs to '\@', only the ones in strings. To speed your search, however, Perl gives
you the line number of the problem line(s) in its error message. (To check for them
without accidentally running the program, if that would be a problem, use perl -c script.)
Fix them, and see if it works. |
|||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
