UB Information Technology

Common Gateway Interface - CGI

This page will get you started programming CGI scripts in Perl, for placement on Wings. There are a number of good books available on the Perl programming language; this guide assumes at least some Perl programming experience.

Back-end Server Programs (CGI Scripts)

Most of the information you want to provide on Wings will probably consist of documents that are fairly static. However, you may have services you wish to provide that require some degree of custom processing. For example you may want to have a registration form which people can submit, and have the UBWings server automatically check it for completeness and add the information to a database, which can then be searched by other users. While features such as this are not built into the UBWings server, you can add your own functionality through the use of programs (called "scripts") based on the Common Gateway Interface (CGI) standard.

A CGI script can be any unix-executable program (e.g. written in C, Perl, Unix Shell, or any other language), which uses a standard set of variables to get information from the server, such as the sender's computer address, any form contents submitted, the brand of browser the sender is using, and many others. Your program can then process this and other information, and return results via a custom-built HTML page, an e-mail message, or many other routes. These scripts can be placed anywhere in your UBWings area, with two requirements:

  • they must be world-executable (i.e. chmod a+x)
  • they must have a filename extension of .cgi
Complete information about the CGI standard can be found at: http://hoohoo.ncsa.uiuc.edu/cgi/

Perl Template

Your Perl script should have the extension .cgi, should be world-readable, and should start with (at least) the following lines:
#!/usr/local/bin/perl

require '/net/wings/info/www/cgi-bin/cgi-lib.pl';

&ReadParse;

Getting Input Data

Data can be sent to your script attached to the URL by referencing it as follows:

or by using the GET method from a form. You can then access it in Perl as you would normally reference command-line input.

If you are using the POST method from a form, the data is packaged and sent to the script. It is stored in the %in associative array, and can be accessed as follows:
$variable = $in{'NAME'};
Where NAME is the name of the form field.

Outputting HTML

Usually your CGI scripts will return HTML back to the user, containing links, status messages, etc. Before you can return any HTML, you need to send the HTML content header to the browser, using the following subroutine call:
print &PrintHeader;

After this, you can send through any HTML you want; for example, the following code will print out a few form fields:

print &PrintHeader;

print <<"top";
<html><head>
<title>Generic Form Response</title></head>
<body bgcolor="#ffffff">

<h1>Generic Form Response</h1>
top

print "Name: $in{'NAME'}";
print "Age: $in{'AGE'}";

print "<hr> <a href="home.html">Return home...</a> ";
print "</body></html>";

Writing to Files

Our web server runs all scripts as the userid 'nobody'. In order for your CGI programs to write to files or directories, they either need to be world-writable (gasp) or owned by 'nobody'. To request a change in the ownership of a file or directory, send e-mail to wings@acsu.buffalo.edu, including the full path to the files you'd like changed.

Environmental Variables

A number of environmental variables are stored in the %ENV associative array and can be accessed as follows:
$variable = $ENV{'VAR'};

Where VAR is one of the following:

             AUTH_TYPE,
             CONTENT_LENGTH,
             CONTENT_TYPE,
             DOCUMENT_ROOT,
             GATEWAY_INTERFACE,
             HTTP_ACCEPT,
             HTTP_USER_AGENT,
             PATH,
             PATH_INFO,
             PATH_TRANSLATED,
             QUERY_STRING,
             REMOTE_ADDR,
             REMOTE_HOST,
             REMOTE_IDENT,
             REMOTE_USER,
             REQUEST_METHOD,
             SCRIPT_NAME,
             SERVER_NAME,
             SERVER_PORT,
             SERVER_PROTOCOL,
             SERVER_SOFTWARE

Samples