⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgiprogramming.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: CGI Programming
Date: Wed, 29 Apr 2009 09:28:07 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/cgiprogramming.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>CGI Programming</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3D"Internet Services"=20
href=3D"internetservices.html" rel=3DPREVIOUS><LINK title=3D"Web =
Automation"=20
href=3D"webautomation.html" rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/internetservices.html">Pr=
ev</A></TD>
    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/webautomation.html">Next<=
/A></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DCGIPROGRAMMING>19. CGI Programming</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1007>Introduction</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# http://www.perl.com/CPAN/
# http://www.perl.com:8001/bad/mojo.html
# ftp://gatekeeper.dec.com/pub/misc/netlib.tar.Z
# ftp://anonymous</FONT><FONT =
color=3D#cdad00>@myplace:gatekeeper</FONT><FONT =
color=3D#bebebe>.dec.com/pub/misc/netlib.tar.Z
# file:///etc/motd
#-----------------------------
# http://mox.perl.com/cgi-bin/program?name=3DJohann&amp;born=3D1685
#-----------------------------
# http://mox.perl.com/cgi-bin/program
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1010>Writing a CGI Script</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/hiweb">download =
the following standalone program</A></FONT>
#!/usr/bin/perl -w
# hiweb - load CGI module to decode information given by web server
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>strict</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI</FONT> <FONT =
color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>:standard =
escapeHTML<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe># get a parameter from a form
</FONT><FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$value</FONT> =3D param<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>'PARAM_NAME'</FONT><FONT =
color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe># output a document
</FONT><FONT color=3D#ff7f50>print</FONT> header<FONT =
color=3D#00ffff>()</FONT>, start_html<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Howdy there!"</FONT><FONT color=3D#00ffff>)</FONT>,
      p<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"You typed: =
"</FONT>, tt<FONT color=3D#00ffff>(</FONT>escapeHTML<FONT =
color=3D#00ffff>(</FONT>$value<FONT color=3D#00ffff>)))</FONT>,
      end_html<FONT color=3D#00ffff>();</FONT>

<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI</FONT> =
<FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>:standard<FONT color=3D#00ffff>);</FONT>
$who   =3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Name"</FONT><FONT color=3D#00ffff>);</FONT>
$phone =3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Number"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#cdad00>@picks</FONT> =3D param<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Choices"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> header<FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#00cd00>-TYPE</FONT>    =3D&gt; =
<FONT color=3D#00cd00>'text/plain'</FONT>,
              <FONT color=3D#00cd00>-EXPIRES</FONT> =3D&gt; <FONT =
color=3D#00cd00>'+3d'</FONT> <FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1013>Redirecting Error Messages</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>CGI::Carp</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"This is a =
complaint"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"But this one is =
serious"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>BEGIN</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>CGI::Carp</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>carpout<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>LOG, =
<FONT =
color=3D#00cd00>"&gt;&gt;/var/local/cgi-logs/mycgi-log"</FONT><FONT =
color=3D#00ffff>)</FONT>
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Unable to append to mycgi-log: $!\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
    carpout<FONT color=3D#00ffff>(</FONT>*LOG<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>CGI::Carp</FONT> <FONT color=3D#ff7f50>qw</FONT><FONT =
color=3D#00ffff>(</FONT>fatalsToBrowser<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Bad error =
here"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1016>Fixing a 500 Server Error</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/webwhoami">downloa=
d the following standalone program</A></FONT>
#!/usr/bin/perl
# webwhoami - show web users id
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"Content-Type: text/plain\n\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Running as =
"</FONT>, <FONT color=3D#ff7f50>scalar</FONT> <FONT =
color=3D#98fb98>getpwuid</FONT><FONT color=3D#00ffff>(</FONT>$&gt;<FONT =
color=3D#00ffff>)</FONT>, <FONT color=3D#00cd00>"\n"</FONT><FONT =
color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe>#-----------------------------
#% perl -wc cgi-script        # just compilation
#
#% perl -w  cgi-script        # parms from stdin
#(offline mode: enter name=3Dvalue pairs on standard input)
#
#name=3Djoe
#
#number=3D10
#
#^D
#
#
#% perl -w  cgi-script name=3Djoe number=3D10    # run with mock form =
input
#% perl -d  cgi-script name=3Djoe number=3D10    # ditto, under the =
debugger
#
## POST method script in csh
#% (setenv HTTP_METHOD POST; perl -w cgi-script name=3Djoe number=3D10)
## POST method script in sh
#% HTTP_METHOD=3DPOST perl -w cgi-script name=3Djoe number=3D10
#-----------------------------
#% perl -MCGI -le 'print CGI-&gt;VERSION'
#2.49
#-----------------------------
</FONT>$| =3D <FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1019>Writing a Safe CGI Program</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
#!/usr/bin/perl -T
</FONT><FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT color=3D#00cd00>"&gt; </FONT><FONT =
color=3D#cdad00>$ARGV</FONT><FONT color=3D#00cd00>[0]"</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# Insecure dependency in open while running with -T switch at ...
#-----------------------------
</FONT>$file =3D <FONT color=3D#cdad00>$ARGV</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>];</FONT>                                   <FONT =
color=3D#bebebe># $file tainted
</FONT><FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT>$file =3D~ <FONT color=3D#ff7f50>m</FONT><FONT =
color=3D#bebebe>#^([\w.-]+)$#) {                  # $1 is untainted
</FONT>    <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"filename '$file' has invalid characters.\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
$file =3D $<FONT color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>  =
                                       <FONT color=3D#bebebe># $file =
untainted
#-----------------------------
</FONT><FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#b2dfee>-e</FONT> $filename<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>                  =
   <FONT color=3D#bebebe># WRONG!
</FONT>    <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>FH, <FONT color=3D#00cd00>"&gt; =
$filename"</FONT><FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#bebebe># ...
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1022>Making CGI Scripts =
Efficient</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
#Alias /perl/ /real/path/to/perl/scripts/
#
#&lt;Location /perl&gt;
#SetHandler  perl-script
#PerlHandler Apache::Registry
#Options ExecCGI
#&lt;/Location&gt;
#
#PerlModule Apache::Registry
#PerlModule CGI
#PerlSendHeader On
#-----------------------------
#&lt;Files *.perl&gt;
#SetHandler  perl-script
#PerlHandler Apache::Registry
#Options ExecCGI
#&lt;/Files&gt;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -