So far in this tutorial, we've looked at some of the ways in which you can create pages with lists, image maps and a few other features, which should give you the basis of some fairly clever pages. But look around the Web, and you'll see that there are a lot more tricks that can be done, like pages where you can fill in all your details, and click on the send button to request a catalogue, or search a database.
The way all this is handles is with two important features - forms and scripts. We've already seen how a script is called when you use an imagemap, but the scripting system for web pages is much more sophisticated than that. It's based around something called CGI - the Common Gateway Interface - which is designed to make sure that you don't have to learn a different way of writing scripts depending on which type of web server that you're using. If a script was written for the NCSA web server, it should work with the CERN one, and vice-versa. If you're using a web server that's running on a different type of computer, like OS/2, Windows or Macintosh, there may be some differences, but the basic principles are pretty much the same.
Whatever sort of web server you're using, scripts have one important thing in common - whatever the script prints out on the screen when it's run as a program is what will be sent back to the browser when someone clicks on a link that activates it. So, you'll often be able to test your script before installing it on a web site, to make sure that it produces the right messages.
However, there's one important thing that any script has to do before it can send information destined for the browser - it has to say what sort of information is being sent back - which can be either text or HTML. That's all done by including lines at the start of your script that specify the Content-Type. Here's an example, which would work as both a DOS batch file or a UNIX shell script, and tells the browser that the rest of the information should be treated as pre-formatted text
echo Content-Type text/plain echo
The second 'echo' is important - there must be a blank line between the Content-Type header and the rest of the output from your script. If you want it to display HTML, just change the content type from text/plain to text/html. For example, if you include a link that looks like this
then the script below will produce a display like the one shown in the screenshot.
echo Content-Type: text/html echo echo "<html><head><title>The time is now ...</title></head><body>" echo "<h1>Current time on this server</h1>" date echo "<P>Thank you for calling. Have a nice day<P>" echo "</body></html>" exit
To make the script run, you'll have to put it in the scripts directory on your web server; the /cgi-bin/ at the beginning of the URL for the script is a shorthand, which the server automatically translates to the real name of the directory (and on an NCSA server, you might need to use '/htbin/' instead). Depending on the way your server has been set up, that might be a shared directory, or it could be a directory in your own private web-space. If you rent web space on a commercial server, remember that you might not always have permission to run scripts, so if you want to use them to spice up your pages, check before signing on the dotted line.
So, now you know how to send output from the script to the browser, what about the opposite direction? It's not quite so straightforward, but once you've got the hang of it, things should be pretty simple. When information is sent from a browser to a script via CGI, it's built up into a query string (sometimes saved in a variable called QUERY_STRING). You'll probably have seen a few query strings without realising, when you've clicked on some types of link, and seen your browser display a URL that looks something like
Everything after the question mark is the query string, which consists of a series of variables and their values, separated by & symbols. Your script can use those variables to control what it does - which could be displaying a personalised message on the browser, or simply making an entry in a file to log who's been accessing your pages.
Depending on which web server you're running, and which language your script is written in, there are different ways of actually turning the information from the query string into something that can be used easily. But before we look at that, let's see how you get the information from the browser in the first place.