This simple Perl CGI program will display all of the environment variables that are being passed around. Two similar programs are included in the cgi-bin directory of the Apache distribution. Note that some variables are required, while others are optional, so you may see some variables listed that were not in the official list. In addition, Apache provides many different ways for you to add your own environment variables to the basic ones provided by default.
The program then can process that data as though it was coming in from the keyboard, or from a file. The "special format" is very simple. Inconvenient characters like spaces, ampersands, and equals signs, are converted into their hex equivalent so that they don't gum up the works.
The whole data string might look something like:. You'll sometimes also see this type of string appended to a URL. That's called a GET request. Your program is then responsible for splitting that string up into useful information.
Fortunately, there are libraries and modules available to help you process this data, as well as handle other of the aspects of your CGI program. When you write CGI programs, you should consider using a code library, or module, to do most of the grunt work for you. This leads to fewer errors, and faster development. The most popular module for this purpose is CGI.
You might also consider CGI::Lite , which implements a minimal set of functionality, which is all you need in most programs. If you're writing CGI programs in C, there are a variety of options. When you post a question about a CGI problem that you're having, whether to a mailing list, or to a newsgroup, make sure you provide enough information about what happened, what you expected to happen, and how what actually happened was different, what server you're running, what language your CGI program was in, and, if possible, the offending code.
This will make finding your problem much simpler. Note that questions about CGI problems should never be posted to the Apache bug database unless you are sure you have found a problem in the Apache source code. Copyright The Apache Software Foundation.
Licensed under the Apache License, Version 2. What's going on behind the scenes? Note: If Apache has been built with shared module support you need to ensure that the module is loaded; in your httpd. But it's still not working! There are four basic things that you may see in your browser when you try to access your CGI program from the web: The output of your CGI program Great!
That means everything worked fine. If the output is correct, but the browser is not processing it correctly, make sure you have the correct Content-Type set in your CGI program. Reread the section on configuring Apache and try to find what you missed. A message starting with "Forbidden" That means that there is a permissions problem.
Check the Apache error log and the section below on file permissions. A message saying "Internal Server Error" If you check the Apache error log , you will probably find that it says "Premature end of script headers", possibly along with an error message generated by your CGI program. In this case, you will want to check each of the below sections to see what might be preventing your CGI program from emitting the proper HTTP headers.
File permissions Remember that the server does not run as you. When editing CGI scripts on Windows, end-of-line characters may be appended to the interpreter path. Failure to do so may result in "Command not found" warnings from the OS, due to the unrecognized end-of-line character being interpreted as a part of the interpreter filename. Environment variables Environment variables are values that float around you as you use your computer.
The difference is that when a server recognizes that the address being requested is a CGI program, the server does not return the file contents verbatim.
Instead, the server tries to execute the program. Here is what a sample client request might look like:. Since the server is configured to recognize all files inf the cgi-bin directory tree as CGI programs, it understands that it should execute the program instead of relaying it directly to the browser.
All this information is made available to the CGI program, along with additional information from the server. The way that CGI programs get their input depends on the server and on the native operating system. These variables store such information as the input search string in the case of a form , the format of the input, the length of the input in bytes , the remote host and user passing the input, and other client information. They also store the server name, the communication protocol, and the name of the software running the server.
Once the CGI program starts running, it can either create and output a new document, or provide the URL to an existing one. The data stream consists of two parts. The first part is either a full or partial HTTP header that at minimum describes what format the returned data is in e. A blank line signifies the end of the header section. The second part is the body, which contains the data conforming to the format type reflected in the header.
The body is not modified or interpreted by the server in any way. A CGI program can choose to send the newly created data directly to the client or to send it indirectly through the server. If the output consists of a complete HTTP header, the data is sent directly to the client without server modification. It's actually a little more complicated than this, as we will discuss in Chapter 3 , Output from the Common Gateway Interface.
Or, as is usually the case, the output is sent to the server as a data stream. The server is then responsible for adding the complete header information and using the HTTP protocol to transfer the data to the client. The header contains the communication protocol, the date and time of the response, the server name and version, and the revision of the MIME protocol. Now, the output with the partial HTTP header:. In this instance, the only header line that is output is the Content-type header, which describes the MIME format of the output.
Most CGI programmers prefer to supply only a partial header. It is much simpler to output the format and the data than to formulate the complete header information, which can be left to the server.
However, there are times when you need to send the information directly to the client by outputting a complete HTTP header , as you will see in Chapter 3 , Output from the Common Gateway Interface. Before you can run CGI programs on your server, certain parameters in the server configuration files must be modified. You can have multiple directories to hold CGI scripts:. You might wonder why all CGI programs must be placed in distinct directories.
The most important reason for this is system security. By having all the programs in one place, a server administrator can control and monitor all the programs being run on the system. However, there are directives that allow programs to be run outside of these directories, based on the file extension. The following directives, when placed in the srm. However, this could be very dangerous! By globally enabling all files ending in certain extensions, there is a risk that novice programmers might write programs that violate system security e.
Before choosing a language, you must consider the following features:. Let's look at each of these features in more detail. Most CGI applications involve manipulating text as you will see throughout this book some way or another, so inherent pattern matching is very important.
The ability of a language to interface with other software, such as databases, is also very important. This greatly enhances the power of the Web by allowing you to write gateways to other information sources, such as database engines or graphic manipulation libraries.
Finally, the last attribute that must be taken into account is the ease with which the language can access environmental variables. These variables constitute the input to the CGI program, and thus are very important. Here is a quick review of the advantages and, in some cases, disadvantages of each one.
Since the advent of System 7. Though AppleScript lacks inherent pattern-matching operators, certain extensions have been written to make it easy to handle various types of data. AppleScript also has the power to interface with other Macintosh applications through AppleEvents.
For example, a Mac CGI programmer can write a program that presents a form to the user, decode the contents of the form, and query and search a Microsoft FoxPro database directly through AppleScript.
In addition, these languages lack database extensions and inherent pattern-matching abilities, although modules and functions can be written to achieve these functions. C Shell lacks pattern-matching operators, and so other UNIX utilities, such as sed or awk , must be used whenever you want to manipulate string information.
However, there is a software tool, called uncgi and written in C, that decodes form data and stores the information into shell environment variables, which can be accessed rather easily. Obviously, communicating with a database directly is impossible, unless it is done through a foreign application. Finally, the C Shell has some serious bugs and limitations that make using it a dangerous proposition for the beginner.
Perl is by far the most widely used language for CGI programming! This web page is used to edit or change the system password. The web page is stored in three files. The third the main file is the script file that generates dynamic data: system. It contains the following:. This is done using. It is not changed when the web page generates but is simply included in the script using. This is how the generated web page looks like, with the dynamically generated items in the Setting column:.
You can see the HTML source code of this web page. This data is actually sent to the web client when the client requests the web page system.
0コメント