[Index] [Up] [Back] [Next]

6.1 Using ISINDEX for server-side searches

Doing searches with ISINDEX is not difficult, bit can appear tricky. The first thing to realize is that a server does not know how to do a search. Instead it must access a search engine, which is simply a program designed to search files or databases. The interface between such search programs and the http server is a script or program, usually placed in the server's "cgi-bin" directory. These scripts are accessed via URLs such as http://www.foo.com/cgi-bin/foo, where foo is the name of the script or program, and the /cgi-bin/ path is a special path that references the directory containing the special programs and scripts that can be executed by the server.

Example usage of ISINDEX

I have the file /u/www/Webdocs/Personnel on my http server. I want to allow someone to search this file for names, using a WWW browser, and I want to do this using the ISINDEX element.

1. The Server-side Script

Step one is to create a script to interface the server and browser with the search program (here a program named grep). My script is srch-example, found in my server's cgi-bin directory. This is accessed via the URL
http://www.utirc.utoronto.ca/cgi-bin/srch-example. Here is the content of srch-example:
#!/bin/sh
echo Content-type: text/html
echo
if [ $# = 0 ]
then
  echo "<HEAD>"
  echo "<TITLE>UTIRC Phonebook Search</TITLE>"
  echo "<ISINDEX>"
  echo "</HEAD>"
  echo "<BODY>"
  echo "<H1>UTIRC Phonebook Search</H1>"
  echo "Enter your search in the search field.<P>"
  echo "This is a case-insensitive substring search: thus"
  echo "searching for 'ian' will find 'Ian' and Adriana'."
  echo "</BODY>"
else
  echo "<HEAD>"
  echo "<TITLE>Result of search for \"$*\".</TITLE>"
  echo "</HEAD>"
  echo "<BODY>"
  echo "<H1>Result of search for \"$*\".</H1>"
  echo "<PRE>"
  grep -i "$*" /u/www/Webdocs/Personnel
  echo "</PRE>"
  echo "</BODY>"
fi

2. How Does This Script Work?

We assume that someone has accessed the URL
http://www.utirc.utoronto.ca/cgi-bin/srch-example
What happens? When the script is accessed it always echoes the line Content-type: text/html. This is sent back to the browser, so that the echoed material becomes the new document. In particular this line tells the browser to expect a text/html document.

3. ISINDEX Signals a Search

the if statement checks to see if there are any arguments to the script. Arguments are passed from the browser to the server script via the URL: arguments to be passed to the script are added to the end of the URL, separeted from the regular URL by a question mark. In our case there are no arguments so we execute the first branch of the if. This echoes some standard HTML definitions, and then sends the ISINDEX code. This tells the browser that this is a search.

The browser display the received document and prompts you for a search string. For example, Mosaic will present a fill-in template, where you type the desired search string. When you press return, the browser accesses the same URL as before, but now appends the appropriate search string. For example, if I filled in the form with my name (Ian) the accessed URL is now

http://www.utirc.utoronto.ca/cgi-bin/srch-example?ian

4. Second URL Access: Search Results

The above URL again accesses srch-example, but this time with an argument (Ian), so that the second branch of the if is executed. This branch echoes new headings, indicating what was searched for, and runs the grep program to search the file. By default the output of grep is echoed, so the search results are sent to the browser. ISINDEX is NOT added here, as this branch provides the results of the search. The final result is a document containing the search results.


DEMONSTRATION

That is, briefly, the whole story. You can test this example and see this script in action by accessing a appropriate test URL.

[Index] [Up] [Back] [Next]