E-mail and Internet Accounts Guides

Office of Information Technology

What's Inside

OIT Resources

ADCS Home > Online Guides > Personal Web Pages > CGI FAQ

CGI Frequently Asked Questions FAQ

This site was designed to help users of the University of Minnesota's web services. More precisely, it is here to provide help with CGI (Common Gateway Interface) scripts. CGI is used for a wide variety of World Wide Web applications. Some popular uses include counters, guestbooks, loggers, and form submissions.

What is a CGI script?

Where do I put CGI scripts?

Why won't my Perl script run in the UNIX shell?

How do I access a CGI script over the Web?

What do I do about a "Server Error"?

My script runs, but it messes up the variables that are sent to it.

Where can I find configurable CGI scripts?

My script runs in the UNIX shell, but I still get a server error!


What is a CGI script?

A CGI script is a text file that is interpreted and executed by a server. Usually this is the same server as the one that serves your web pages. It is a simple program that can provide useful information to both the web-author and the web-reader. One popular programming language for CGI scripts is Perl (Practical Extraction and Report Language). Other languages may also be used.

Back to FAQ list


Where do I put a CGI script?

Just as your web documents need to be stored in the "web-docs" directory of your account, CGI scripts need to be stored in the directory named "cgi-bin". More specifically, the directory is: /www/YourInternetID/cgi-bin, where the term YourInternetID is your Internet ID (e.g. user0001).

Back to FAQ list


Why won't my Perl script run in the UNIX shell?

In order for a Perl script to run it needs to know the location of the Perl program on the machine it is on. This is specified in the first line of the script in the form:
#!/full/path/to/perl.
Three of the most common are:
/usr/local/bin/perl -- this links to perl version 5.6.1 as of Nov. 6, 2003
/usr/bin/perl -- same version of Perl as above
If your script needs a specific version of Perl you may want to look in the /usr/local/bin/ directory for another version. All the new versions are listed with their version in their name such as perl5.004 and perl5.003. To assign one of these versions simply use:
/usr/local/bin/perlX -- where X is the version.
If you are unsure which one to use, /usr/local/bin/perl is usually the default.

Back to FAQ list


How do I access a CGI script over the Web?

The URL (Uniform Resource Locator), often called location, you must use to access the CGI scripts saved in your "cgi-bin" directory is as follows:

http://www.tc.umn.edu/cgi-bin/do?user=YourInternetID&prog=ScriptFileName
where:
YourInternetID is your internet ID, and
ScriptFileName is the name of your CGI script file.

As you may have guessed, you can access a CGI script from within a web page with the <A HREF="ScriptURL"> tag (a link). However, currently you cannot use Server-Side Includes with our server. Many CGI scripts require special environment variables, set by the web server upon script execution, in order to run.

Back to FAQ list


What do I do about a "Server Error"?

This is the most popular of errors you will encounter using cgi. What it means is that the script either won't run, or it won't send the information to your web browser correctly. To see which senario is the case, try running the script in the UNIX environment.

To do this, log into your account using Telnet. That is, telnet to the host: YourInternetID.email.umn.edu. (Where YourInternetID is your University internet ID.) If you are using a Menu Shell, typing a "!" will get you to the UNIX shell. Once in the shell you need to change your working directory to the cgi-bin directory (enter cd cgi-bin). Once in that directory, you can check the syntax of your script by typing perl -c ScriptFileName. Where ScriptFileName is the name of your script. If the syntax is correct, try running the script directly in the shell. To do this simply type the script's name at the prompt. This should print out the HTML file that the script produces. If this output is wrong and you wish to try to configure the script to run correctly anyway, you can try to edit it.

HINT: To edit the script at the shell, you can use the Pico text editor by typing pico ScriptFileName

Back to FAQ list


My script runs, but it messes up the variables that are sent to it.

Since most scripts are not written to be run from the "do" script, you usually have to edit the script. More specifically, you have to alter the way the script sees the variable named $ENV{'QUERY_STRING} which becomes everything after http://www.tc.umn.edu/cgi-bin/do? in the the location you specifed to run the script.

Back to FAQ list


Where can I find configurable CGI scripts?

There are several places on the Web where to find written CGI scripts. However, they are not perfect, and are more than likely going to require a good bit of configuration.

Back to FAQ list


My script runs in the UNIX shell, but I still get a server error!

This error is given when your script prints out something other than:

Content-type: text/html
<blank line>
<blank line>
<your HTML output>

which should look like this in Perl:

print "Content-type: text/html\n\n";

That's where the error comes from, but that's just the tip of the iceberg. Usually, the reason the above does not print is because perl prints out an error message before it gets the chance to print this header.

1).

A good place to start is to run the script in the shell. However, the shell does not pass the same environment variables to the script that the web server does when the script is executed via CGI. For example, when CGI-executed, the web server passes in variables describing the remote computer and sometimes the values of your CGI form fields. In Perl, you would pass variableName/value pairs from the shell to the scipt like this:

perl YourScriptFileName SomeVariableName=SomeValue AnotherVariableName=AnotherValue

Example:

perl email.pl sender=user0123@tc.umn.edu subject=test

2).

If your script still prints out the correct document in the UNIX shell, start thinking about file permissions. If your script writes in a data file, (like a log file), chances are the file is not writable by your user ID. To ensure that you have write access to the file, change its mode:

chmod u+rwx YourDataFile

Meaning:

change the mode of YourDataFile so that processes running under your user(u) privileges can read(r) it, write(w) in it, and execute(x) it

Back to FAQ list

This document was last modified on Monday, 27-Jun-2005 16:38:30 CDT
The URL of this document is http://www1.umn.edu/adcs/help/cgifaq.html