Cgi header file name
The server will retrieve the specified document from the Web, giving the appearance that the client had not requested your CGI program, but that document see Figure 3. A common use for this feature is to return a generic document that contains static information.
For example, say you have a form for users to fill out, and you want to display a thank-you message after someone completes the form. You can have the CGI program create and display the message each time it is called. But a more efficient way would be for the program to send instructions to the server to redirect and retrieve a file that contains a generic thank-you message. Suppose you have an HTML file thanks. You could use the programs discussed earlier to return static documents, but it would be counterproductive to do it in that manner.
Instead, it is much quicker and simpler to do the following:. The server will return the HTML file thanks. You don't have to worry about returning the MIME content type for the document; it is taken care of by the server.
An important thing to note is that you cannot return any content type headers when you are using server redirection.
You can use server redirection to your advantage and design CGI applications like the following:. This program checks the load average of the host system with the uptime command see Chapter 1 , The Common Gateway Interface CGI for an explanation of the regular expression. And the last thing to note is that you are not limited to returning documents on your own server.
You can also return a document static or virtual located elsewhere on the Internet, so long as it has a valid URL:. Most browsers cache or store internally the documents you access. This is a very positive feature that saves a lot of resources; the browser doesn't have to retrieve the document every time you look at it. However, it can be a slight problem when you are dealing with virtual documents created by CGI programs.
Once the browser accesses a virtual document produced by a CGI program, it will cache it. The next time you try to access the same document, the browser will not make a request to the server, but will reload the document from its cache.
To see the effects of caching, try running the following program:. This program displays the current time, as well as a hypertext link to itself. If you click on the link to run the program again, the date and time that is displayed should change, but it does not, because the browser is retrieving the cached document.
You need to explicitly tell the browser to reload the document if you want to run the CGI program again. Fortunately, there is a solution to this problem. Status codes are used by the HTTP protocol to communicate the status of a request. CGI programs can send status information as part of a virtual document. Here's an arbitrary example that returns success if the remote host name is bu.
The Status header consists of a three-digit numerical status code, followed by a string representing the code. A status value of indicates success, while a value of constitutes a bad request. In addition to these two, there are numerous other status codes you can use for a variety of situations, ranging from an unauthorized or forbidden request to internal system errors. Major features including processing form submissions, file uploads, reading and writing cookies, query string generation and manipulation, and processing and preparing HTTP headers.
It has the benefit of having developed and refined over 20 years with input from dozens of contributors and being deployed on thousands of websites. If you upgrade to a new version of perl or if you rely on a system or vendor perl and get an updated version of perl through a system update, then you will have to install CGI. To make this a little easier the CGI::Fast module has been split into its own distribution, meaning you do not need access to a compiler to install CGI.
The rationale for this decision is that CGI. There are far better, cleaner, quicker, easier, safer, more scalable, more extensible, more modern alternatives available at this point in time. These will be documented with CGI::Alternatives. Note that the v4 releases of CGI. If you plan to upgrade to v4. Any issues, bugs, or patches will be rejected unless they relate to fundamentally broken page rendering.
You should be using a template engine for better separation of concerns. These functions, and perldoc for them, are considered deprecated, they are no longer being maintained and no fixes or features for them will be accepted. They will, however, continue to exist in CGI.
There are two styles of programming with CGI. In the object-oriented style you create one or more CGI objects and then use object methods to create the various elements of the page. You can modify the objects, save them to a file or database and recreate them. Because each object corresponds to the "state" of the CGI script, and because each object's parameter list is independent of the others, this allows you to save the state of the script and restore it later. In the function-oriented style, there is one default CGI object that you rarely deal with directly.
Instead you just call functions to retrieve CGI parameters, manage cookies, and so on. The following example is identical to above, in terms of output, but uses the function-oriented interface.
The main differences are that we now need to import a set of functions into our name space usually the "standard" functions , and we don't need to create the CGI object. The examples in this document mainly use the object-oriented style. Most CGI. To simplify this interface, all routines use a named argument calling style that looks like this:. Each argument name is preceded by a dash. In fact, only the first argument needs to begin with a dash.
If a dash is present in the first argument CGI. Several routines are commonly called with just one argument. In the case of these routines you can provide the single argument without an argument name. In this case, the single argument is the document type. Sometimes named arguments expect a scalar, sometimes a reference to an array, and sometimes a reference to a hash. Often, you can pass any type of argument and the routine will do whatever is most appropriate.
For example, the param routine is used to set a CGI parameter to a single or a multi-valued value. The two cases are shown below:. Many routines will do something useful with a named argument that it doesn't recognize. For example, you can produce non-standard HTTP header fields by providing them as named arguments:. If you provide a file handle to the new method, it will read parameters from the file or STDIN, or whatever.
The file can be in any of the forms describing below under debugging i. Conveniently, this type of file is created by the save method see below. Multiple records can be saved and restored. Perl purists will be pleased to know that this syntax accepts references to file handles, or even references to filehandle globs, which is the "official" way to pass a filehandle. This will re initialize the default CGI object from the indicated file handle. If the script was invoked as the result of an ISINDEX search, the parsed keywords can be obtained as an array using the keywords method.
If the script was invoked with a parameter list e. The array of parameter names returned will be in the same order as they were submitted by the browser. Usually this order is the same as the order in which the parameters are defined in the form however, this isn't part of the spec, and so isn't guaranteed. When calling param If the parameter is multivalued e. Otherwise the method will return the first value.
Warning - calling param in list context can lead to vulnerabilities if you do not sanitise user input as it is possible to inject other param keys and values into your code. The following code is an example of a vulnerability as the call to param will be evaluated in list context and thus possibly inject extra keys and values into the hash:. If you call param in list context with an argument a warning will be raised by CGI.
If the parameter does not exist at all, then param will return undef in scalar context, and the empty list in a list context. This sets the value for the named parameter 'foo' to an array of values.
This adds a value or list of values to the named parameter. The values are appended to the end of the parameter if it already exists. Otherwise the parameter is created. Note that this method only recognizes the named argument calling syntax.
This creates a series of variables in the 'R' namespace. For keyword lists, a variable R::keywords will appear. If no namespace is given, this method will assume 'Q'. NOTE 1: Variable names are transformed as necessary into legal perl variable names. All non-legal characters are transformed into underscores.
If you need to keep the original names, you should use the param method instead to access CGI variables by name.
In fact, you should probably not use this method at all given the above caveats and security risks. This completely clears a list of parameters. It sometimes useful for resetting parameters that you don't want passed down between script invocations. If you are using the function call interface, use "Delete " instead to avoid conflicts with perl's built-in delete operator. This clears the CGI object completely. It might be useful to ensure that all the defaults are taken when you create a fill-out form.
To retrieve it, use code like this:. If you don't know what the preceding means, worry not. This will return an array reference to the named parameter, which you then can manipulate in any way you like. Many people want to fetch the entire parameter list as a hash in which the keys are the names of the CGI parameters, and the values are the parameters' values. The Vars method does this. Called in a scalar context, it returns the parameter list as a tied hash reference.
Changing a key changes the value of the parameter in the underlying CGI parameter list. Called in a list context, it returns the parameter list as an ordinary hash.
This allows you to read the contents of the parameter list, but not to change it. When using this, the thing you must watch out for are multivalued CGI parameters. You must split this packed string in order to get at the individual values.
This is the convention introduced long ago by Steve Brenner in his cgi-lib. This will write the current state of the form to the provided filehandle. You can read it back in by providing a filehandle to the new method. Note that the filehandle can be a file, a pipe, or whatever. Both name and value are URL escaped. Multi-valued CGI parameters are represented as repeated names. You can write out multiple records and read them back in with several calls to new. You can do this across several sessions by opening the file in append mode, allowing you to create primitive guest books, or to keep a history of users' queries.
Here's a short example of creating multiple session records:. See Boulder for further details. Errors can occur while processing user input, particularly when processing uploaded files. When these errors occur, CGI will stop processing and return an empty parameter list. The error messages are formatted as HTTP status codes. You can either incorporate the error text into a page, or use it as the value of the HTTP status:. When using the function-oriented interface see the next section , errors may only occur the first time you call param.
Be ready for this! To use the function-oriented interface, you must specify which CGI. There is a small overhead associated with this importation, but it isn't much. The listed methods will be imported into the current package; you can call them directly without creating a CGI object first. This example shows how to import the param and header methods, and then use them directly:. More frequently, you'll import common sets of functions by referring to the groups by name.
All function sets are preceded with a ":" character as in ":cgi" for CGI protocol handling methods. Import all the available methods. For the full list, see the CGI. Note that in the interests of execution speed CGI. This may change in the future. In addition to the function sets, there are a number of pragmas that you can import.
Pragmas, which are always preceded by a hyphen, change the way that CGI. Pragmas, function sets, and individual functions can all be imported in the same use line.
For example, the following use statement imports the cgi set of functions and enables debugging mode pragma -debug :. This makes CGI. If you do not use this option you can manually select which fields are expected to return utf-8 strings and convert them using code like this:. If you pass an argument to the method, the property value will be set, and also the current object itself will be returned; therefore you can chain methods as follows:.
If no argument is supplied, the property value will be returned. If the given property doesn't exist, undef will be returned.
Get or set the attachment property. Can be used to turn the page into an attachment. Represents suggested name for the saved file. Get or set the cookies property. Get or set the expires property.
The Expires header gives the date and time after which the entity should be considered stale. You can specify an absolute or relative expiration interval. The following forms are all valid for this field:. Get or set the nph property. If set to a true value, will issue the correct headers to work with a NPH no-parse-header script.
Get or set the p3p property. The parameter can be an arrayref or a space-delimited string. This module converts them as follows:. Since Blosxom depends on the procedural interface of CGI.
It's up to you to decide how to manage HTTP cookies. The following method behaves like Mojo::Message::Response 's cookies method:. If you don't want to send the Content-Type header, set the type property to an empty string, though it's far from intuitive manipulation:.
0コメント