📄 cgi.3
字号:
use suEXEC or a \s-1CGI\s0 wrapper program to run your script. The temporaryfile is created with mode 0600 (neither world nor group readable)..SpThe temporary directory is selected using the following algorithm:.Sp.Vb 2\& 1. if the current user (e.g. "nobody") has a directory named\& "tmp" in its home directory, use that (Unix systems only).\&\& 2. if the environment variable TMPDIR exists, use the location\& indicated.\&\& 3. Otherwise try the locations /usr/tmp, /var/tmp, C:\etemp,\& /tmp, /temp, ::Temporary Items, and \eWWW_ROOT..Ve.SpEach of these locations is checked that it is a directory and iswritable. If not, the algorithm tries the next choice..Sh "\s-1SPECIAL\s0 \s-1FORMS\s0 \s-1FOR\s0 \s-1IMPORTING\s0 HTML-TAG \s-1FUNCTIONS\s0".IX Subsection "SPECIAL FORMS FOR IMPORTING HTML-TAG FUNCTIONS"Many of the methods generate \s-1HTML\s0 tags. As described below, tagfunctions automatically generate both the opening and closing tags.For example:.PP.Vb 1\& print h1(\*(AqLevel 1 Header\*(Aq);.Ve.PPproduces.PP.Vb 1\& <h1>Level 1 Header</h1>.Ve.PPThere will be some times when you want to produce the start and endtags yourself. In this case, you can use the form start_\fItag_name\fRand end_\fItag_name\fR, as in:.PP.Vb 1\& print start_h1,\*(AqLevel 1 Header\*(Aq,end_h1;.Ve.PPWith a few exceptions (described below), start_\fItag_name\fR andend_\fItag_name\fR functions are not generated automatically when you\&\fIuse \s-1CGI\s0\fR. However, you can specify the tags you want to generate\&\fIstart/end\fR functions for by putting an asterisk in front of theirname, or, alternatively, requesting either "start_\fItag_name\fR\*(L" or\&\*(R"end_\fItag_name\fR" in the import list..PPExample:.PP.Vb 1\& use CGI qw/:standard *table start_ul/;.Ve.PPIn this example, the following functions are generated in addition tothe standard ones:.IP "1. \fIstart_table()\fR (generates a <table> tag)" 4.IX Item "1. start_table() (generates a <table> tag)".PD 0.IP "2. \fIend_table()\fR (generates a </table> tag)" 4.IX Item "2. end_table() (generates a </table> tag)".IP "3. \fIstart_ul()\fR (generates a <ul> tag)" 4.IX Item "3. start_ul() (generates a <ul> tag)".IP "4. \fIend_ul()\fR (generates a </ul> tag)" 4.IX Item "4. end_ul() (generates a </ul> tag)".PD.SH "GENERATING DYNAMIC DOCUMENTS".IX Header "GENERATING DYNAMIC DOCUMENTS"Most of \s-1CGI\s0.pm's functions deal with creating documents on the fly.Generally you will produce the \s-1HTTP\s0 header first, followed by thedocument itself. \s-1CGI\s0.pm provides functions for generating \s-1HTTP\s0headers of various types as well as for generating \s-1HTML\s0. For creating\&\s-1GIF\s0 images, see the \s-1GD\s0.pm module..PPEach of these functions produces a fragment of \s-1HTML\s0 or \s-1HTTP\s0 which youcan print out directly so that it displays in the browser window,append to a string, or save to a file for later use..Sh "\s-1CREATING\s0 A \s-1STANDARD\s0 \s-1HTTP\s0 \s-1HEADER:\s0".IX Subsection "CREATING A STANDARD HTTP HEADER:"Normally the first thing you will do in any \s-1CGI\s0 script is print out an\&\s-1HTTP\s0 header. This tells the browser what type of document to expect,and gives other optional information, such as the language, expirationdate, and whether to cache the document. The header can also bemanipulated for special purposes, such as server push and pay per viewpages..PP.Vb 1\& print header;\&\& \-or\-\&\& print header(\*(Aqimage/gif\*(Aq);\&\& \-or\-\&\& print header(\*(Aqtext/html\*(Aq,\*(Aq204 No response\*(Aq);\&\& \-or\-\&\& print header(\-type=>\*(Aqimage/gif\*(Aq,\& \-nph=>1,\& \-status=>\*(Aq402 Payment required\*(Aq,\& \-expires=>\*(Aq+3d\*(Aq,\& \-cookie=>$cookie,\& \-charset=>\*(Aqutf\-7\*(Aq,\& \-attachment=>\*(Aqfoo.gif\*(Aq,\& \-Cost=>\*(Aq$2.00\*(Aq);.Ve.PP\&\fIheader()\fR returns the Content-type: header. You can provide your own\&\s-1MIME\s0 type if you choose, otherwise it defaults to text/html. Anoptional second parameter specifies the status code and a human-readablemessage. For example, you can specify 204, \*(L"No response\*(R" to create ascript that tells the browser to do nothing at all..PPThe last example shows the named argument style for passing argumentsto the \s-1CGI\s0 methods using named parameters. Recognized parameters are\&\fB\-type\fR, \fB\-status\fR, \fB\-expires\fR, and \fB\-cookie\fR. Any other namedparameters will be stripped of their initial hyphens and turned intoheader fields, allowing you to specify any \s-1HTTP\s0 header you desire.Internal underscores will be turned into hyphens:.PP.Vb 1\& print header(\-Content_length=>3002);.Ve.PPMost browsers will not cache the output from \s-1CGI\s0 scripts. Every timethe browser reloads the page, the script is invoked anew. You canchange this behavior with the \fB\-expires\fR parameter. When you specifyan absolute or relative expiration interval with this parameter, somebrowsers and proxy servers will cache the script's output until theindicated expiration date. The following forms are all valid for the\&\-expires field:.PP.Vb 8\& +30s 30 seconds from now\& +10m ten minutes from now\& +1h one hour from now\& \-1d yesterday (i.e. "ASAP!")\& now immediately\& +3M in three months\& +10y in ten years time\& Thursday, 25\-Apr\-1999 00:40:33 GMT at the indicated time & date.Ve.PPThe \fB\-cookie\fR parameter generates a header that tells the browser to providea \*(L"magic cookie\*(R" during all subsequent transactions with your script.Netscape cookies have a special format that includes interesting attributessuch as expiration time. Use the \fIcookie()\fR method to create and retrievesession cookies..PPThe \fB\-nph\fR parameter, if set to a true value, will issue the correctheaders to work with a \s-1NPH\s0 (no-parse-header) script. This is importantto use with certain servers that expect all their scripts to be \s-1NPH\s0..PPThe \fB\-charset\fR parameter can be used to control the character setsent to the browser. If not provided, defaults to \s-1ISO\-8859\-1\s0. As aside effect, this sets the \fIcharset()\fR method as well..PPThe \fB\-attachment\fR parameter can be used to turn the page into anattachment. Instead of displaying the page, some browsers will promptthe user to save it to disk. The value of the argument is thesuggested name for the saved file. In order for this to work, you mayhave to set the \fB\-type\fR to \*(L"application/octet\-stream\*(R"..PPThe \fB\-p3p\fR parameter will add a P3P tag to the outgoing header. Theparameter can be an arrayref or a space-delimited string of P3P tags.For example:.PP.Vb 2\& print header(\-p3p=>[qw(CAO DSP LAW CURa)]);\& print header(\-p3p=>\*(AqCAO DSP LAW CURa\*(Aq);.Ve.PPIn either case, the outgoing header will be formatted as:.PP.Vb 1\& P3P: policyref="/w3c/p3p.xml" cp="CAO DSP LAW CURa".Ve.Sh "\s-1GENERATING\s0 A \s-1REDIRECTION\s0 \s-1HEADER\s0".IX Subsection "GENERATING A REDIRECTION HEADER".Vb 1\& print redirect(\*(Aqhttp://somewhere.else/in/movie/land\*(Aq);.Ve.PPSometimes you don't want to produce a document yourself, but simplyredirect the browser elsewhere, perhaps choosing a \s-1URL\s0 based on thetime of day or the identity of the user..PPThe \fIredirect()\fR function redirects the browser to a different \s-1URL\s0. Ifyou use redirection like this, you should \fBnot\fR print out a header aswell..PPYou should always use full URLs (including the http: or ftp: part) inredirection requests. Relative URLs will not work correctly..PPYou can also use named arguments:.PP.Vb 3\& print redirect(\-uri=>\*(Aqhttp://somewhere.else/in/movie/land\*(Aq,\& \-nph=>1,\& \-status=>301);.Ve.PPThe \fB\-nph\fR parameter, if set to a true value, will issue the correctheaders to work with a \s-1NPH\s0 (no-parse-header) script. This is importantto use with certain servers, such as Microsoft \s-1IIS\s0, whichexpect all their scripts to be \s-1NPH\s0..PPThe \fB\-status\fR parameter will set the status of the redirect. \s-1HTTP\s0defines three different possible redirection status codes:.PP.Vb 3\& 301 Moved Permanently\& 302 Found\& 303 See Other.Ve.PPThe default if not specified is 302, which means \*(L"moved temporarily.\*(R"You may change the status to another status code if you wish. Beadvised that changing the status to anything other than 301, 302 or303 will probably break redirection..Sh "\s-1CREATING\s0 \s-1THE\s0 \s-1HTML\s0 \s-1DOCUMENT\s0 \s-1HEADER\s0".IX Subsection "CREATING THE HTML DOCUMENT HEADER".Vb 8\& print start_html(\-title=>\*(AqSecrets of the Pyramids\*(Aq,\& \-author=>\*(Aqfred@capricorn.org\*(Aq,\& \-base=>\*(Aqtrue\*(Aq,\& \-target=>\*(Aq_blank\*(Aq,\& \-meta=>{\*(Aqkeywords\*(Aq=>\*(Aqpharaoh secret mummy\*(Aq,\& \*(Aqcopyright\*(Aq=>\*(Aqcopyright 1996 King Tut\*(Aq},\& \-style=>{\*(Aqsrc\*(Aq=>\*(Aq/styles/style1.css\*(Aq},\& \-BGCOLOR=>\*(Aqblue\*(Aq);.Ve.PPAfter creating the \s-1HTTP\s0 header, most \s-1CGI\s0 scripts will start writingout an \s-1HTML\s0 document. The \fIstart_html()\fR routine creates the top of thepage, along with a lot of optional information that controls thepage's appearance and behavior..PPThis method returns a canned \s-1HTML\s0 header and the opening <body> tag.All parameters are optional. In the named parameter form, recognizedparameters are \-title, \-author, \-base, \-xbase, \-dtd, \-lang and \-target(see below for the explanation). Any additional parameters youprovide, such as the Netscape unofficial \s-1BGCOLOR\s0 attribute, are addedto the <body> tag. Additional parameters must be proceeded by ahyphen..PPThe argument \fB\-xbase\fR allows you to provide an \s-1HREF\s0 for the <base> tagdifferent from the current location, as in.PP.Vb 1\& \-xbase=>"http://home.mcom.com/".Ve.PPAll relative links will be interpreted relative to this tag..PPThe argument \fB\-target\fR allows you to provide a default target framefor all the links and fill-out forms on the page. \fBThis is anon-standard \s-1HTTP\s0 feature which only works with Netscape browsers!\fRSee the Netscape documentation on frames for details of how tomanipulate this..PP.Vb 1\& \-target=>"answer_window".Ve.PPAll relative links will be interpreted relative to this tag.You add arbitrary meta information to the header with the \fB\-meta\fRargument. This argument expects a reference to an associative arraycontaining name/value pairs of meta information. These will be turnedinto a series of header <meta> tags that look something like this:.PP.Vb 2\& <meta name="keywords" content="pharaoh secret mummy">\& <meta name="description" content="copyright 1996 King Tut">.Ve.PPTo create an HTTP-EQUIV type of <meta> tag, use \fB\-head\fR, describedbelow..PPThe \fB\-style\fR argument is used to incorporate cascading stylesheetsinto your code. See the section on \s-1CASCADING\s0 \s-1STYLESHEETS\s0 for moreinformation..PPThe \fB\-lang\fR argument is used to incorporate a language attribute intothe <html> tag. For example:.PP.Vb 1\& print $q\->start_html(\-lang=>\*(Aqfr\-CA\*(Aq);.Ve.PPThe default if not specified is \*(L"en-US\*(R" for \s-1US\s0 English, unless the \&\-dtd parameter specifies an \s-1HTML\s0 2.0 or 3.2 \s-1DTD\s0, in which case thelang attribute is left off. You can force the lang attribute to leftoff in other cases by passing an empty string (\-lang=>'')..PPThe \fB\-encoding\fR argument can be used to specify the character set for\&\s-1XHTML\s0. It defaults to iso\-8859\-1 if not specified..PPThe \fB\-declare_xml\fR argument, when used in conjunction with \s-1XHTML\s0,will put a <?xml> declaration at the top of the \s-1HTML\s0 header. The solepurpose of this declaration is to declare the character setencoding. In the absence of \-declare_xml, the output \s-1HTML\s0 will containa <meta> tag that specifies the encoding, allowing the \s-1HTML\s0 to passmost validators. The default for \-declare_xml is false..PPYou can place other arbitrary \s-1HTML\s0 elements to the <head> section with the\&\fB\-head\fR tag. For example, to place the rarely-used <link> element in thehead section, use this:.PP.Vb 2\& print start_html(\-head=>Link({\-rel=>\*(Aqnext\*(Aq,\& \-href=>\*(Aqhttp://www.capricorn.com/s2.html\*(Aq}));.Ve.PPTo incorporate multiple \s-1HTML\s0 elements into the <head> section, just pass anarray reference:.PP.Vb 7\& print start_html(\-head=>[ \& Link({\-rel=>\*(Aqnext\*(Aq,\& \-href=>\*(Aqhttp://www.capricorn.com/s2.html\*(Aq}),\& Link({\-rel=>\*(Aqprevious\*(Aq,\& \-href=>\*(Aqhttp://www.capricorn.com/s1.html\*(Aq})
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -