Linguistics 408/508 |
Fall 2003 |
Hammond |
Handout 16
Overview
- Questions from last time
- The basic idea
- Simple cgi
- Forms
The basic idea
- Typically, the client requests a
static document from some server.
The client can request dynamic
content too.
- There are a number of ways to do this: cgi, php,
asp/jsp, servlets, etc. We will focus on the one
you can do most easily with Perl: cgi.
- The basic idea behind cgi is that the URL includes
the name of a program on the server, rather than
the name of a web page. In response to the
client's request, the server actually
runs the program, rather than
sending some static text. What's sent to the
client is the result of running the
program on the server.
Simple cgi
- The simplest kind of example simply prints back
some text (example,
code).
- Getting this to work requires some special stuff.
Moreover, these additional bits are not the same
on different servers.
- Your cgi must be put in a file
with a
.cgi
file
extension, e.g.
simple1.cgi
. (This
is not true on all
systems.)
- You cgi must be put in a specific
directory. On the
syntax
server, this
is
public_html/cgi-bin
.
(This location can vary
depending on the server
configuration.)
- Your cgi program must be
executable. (You can make this
so with the command
chmod
a+x
program.cgi
.)
- If your program prints something
out to be rendered in the
client's browser, it must print
an HTML header
exactly as in the
examples.
- Finally, the
syntax
server can run cgi scripts in a
number of languages. To let it
know that yours is in Perl, you
need to include the "bang" line
at the beginning of the program,
as in the examples, i.e.
#!/usr/bin/perl
.
(This specifies that the cgi
program is written in perl and
the location of perl on the
server.)
- Running a cgi program is different from requesting
a static HTML page. A static page cannot change.
Your cgi program can print out different content
each time it's run (example,
code).
- You can also print out HTML with your cgi program
(example,
code).
Notice that your code is now a curious blend of
Perl and HTML!
Forms
- The basic idea behind forms is that HTML pages can
contain graphical elements that allow you to
input information to a remote cgi
program.
- Let's look at a simple example and then try to
understand it (form, cgi
code).
- You can use this to collect data about how many
people speak how many languages (form, cgi
code).
- The preceding program is very tricky for several
reasons. First, there must be a place to store
data between invocations of the program:
french.txt
. Second, you must set
the read/write privileges so that your cgi
program can write to that file. Generally, only
you can write to files in your directories, but
your cgi program isn't you! Finally, there are
some very tricky points about what happens if
there are multiple simultaneous access of your
cgi program. (See the text for details or talk
to me if you think you'll be doing cgi stuff and
writing to a file.)
- Forms can include lots of input devices, e.g.
lines of text (
type=text
), boxes of
text (textarea
), buttons
(type=submit
), checkboxes
(type=checkbox
), and radioboxes
(type=radio
), etc. Here's an
example that uses all of these (example,
cgi
code).
- You can use the standard
CGI.pm
module to decode all the gibberish in the query
string (example,
cgi
code).