next up previous contents
Next: Beginning CGI Programming in Up: CGI Programming in Perl Previous: How Does CGI Work?

Calling Your CGI Program

The easiest way to run a CGI program is to type in the URL of the program into your Web browser. The Web server should recognize that you are requesting a CGI program and execute it. For example, if you already had a CGI program called test.pl running on a local Web server, you could start it by entering the following URL into your Web browser:

http://localhost/cgi-bin/test.pl

The Web server will execute your CGI script and any output is displayed by your Web browser.

The URL for your CGI program is a virtual path. The actual location of the script on the Web server depends on the configuration of the server software and the type of computer being used. For example, if your computer is running the Linux operating system and the NCSA Web server in a "standard" configuration, then the above virtual would translate into

/usr/local/etc/httpd/cgi-bin/test.pl.

If you were running the Website server under Windows 95, the translated path might be

/website/cgi-shl/test.pl.

If you have installed and are administering the Web server yourself, you probably know where to place your scripts. If you are using a service provider's Web server, ask the server's administrator where to put your scripts and how to reference them from your documents.

There are other ways to invoke CGI programs besides using a Web browser to visit the URL. You can also start CGI programs from:

<A HREF=EF="cgi-bin/test.pl"> Click here to run a CGI program</A>

Interestingly enough, you can pass information to your CGI program by adding extra information to the standard URL. If your CGI program is used for searching your site, for example, you can pass some information to specify which directory to search. The following HTML hyperlink will invoke a search script and tell it to search the /root/document directory.

<A HREF=EF="cgi-bin/search.pl/root/document"> Search the Document Directory <A>

This extra path information can be accessed through the PATH_INFO environment variable.

You can also use a question mark to pass information to a CGI program. Typically, a question mark indicates that you are passing keywords that will be used in a search. <A HREF=EF="cgi-bin/search.pl?Wine+1993">Search for 1993 Wines</A>

The information that follows the question mark will be available to your CGI program through the QUERY_STRING environment variables.

Using either of these approaches will let you create canned CGI requests. By creating these requests ahead of time, you can reduce the amount of typing errors that your users might otherwise have. Later in this chapter, the "CGI and Environment Variables" section discusses all of the environment variables you can use inside CGI programs.

Note Generally speaking, visitors to your Web site should never have to type in the URL for a CGI program. A hypertext link should always be provided to start the program.


next up previous contents
Next: Beginning CGI Programming in Up: CGI Programming in Perl Previous: How Does CGI Work?
dave@cs.cf.ac.uk