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

📄 cgiprogramming.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
#http://somewhere.com:8989/cgi-bin/whatever
#-----------------------------
#% telnet www.perl.com 80
#GET /bogotic HTTP/1.0
#
#<blank line here>
#
#HTTP/1.1 404 File Not Found
#
#Date: Tue, 21 Apr 1998 11:25:43 GMT
#
#Server: Apache/1.2.4
#
#Connection: close
#
#Content-Type: text/html
#
#
#<HTML><HEAD>
#
#<TITLE>404 File Not Found</TITLE>
#
#</HEAD><BODY>
#
#<H1>File Not Found</H1>
#
#The requested URL /bogotic was not found on this server.<P>
#
#</BODY></HTML>
#-----------------------------
</FONT>% GET -esuSU http://mox.perl.com/perl/bogotic
<FONT color=3D#bebebe># GET http://language.perl.com/bogotic
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Host: mox.perl.com
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# User-Agent: lwp-request/1.32
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# GET http://mox.perl.com/perl/bogotic --&gt; 302 Moved Temporarily
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# GET http://www.perl.com/perl/bogotic --&gt; 302 Moved Temporarily
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# GET http://language.perl.com/bogotic --&gt; 404 File Not Found
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Connection: close
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Date: Tue, 21 Apr 1998 11:29:03 GMT
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Server: Apache/1.2.4
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Content-Type: text/html
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Client-Date: Tue, 21 Apr 1998 12:29:01 GMT
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Client-Peer: 208.201.239.47:80
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Title: Broken perl.com Links
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# &lt;HTML&gt;
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# &lt;HEAD&gt;&lt;TITLE&gt;An Error Occurred&lt;/TITLE&gt;&lt;/HEAD&gt;
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# &lt;BODY&gt;
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# &lt;H1&gt;An Error Occurred&lt;/h1&gt;
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# 404 File Not Found
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# &lt;/BODY&gt;
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# &lt;/HTML&gt;
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1037>Managing Cookies</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>$preference_value =3D cookie<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"preference name"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$packed_cookie =3D cookie<FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#00cd00>-NAME</FONT>    =3D&gt; <FONT =
color=3D#00cd00>"preference name"</FONT>,
                         <FONT color=3D#00cd00>-VALUE</FONT>   =3D&gt; =
<FONT color=3D#00cd00>"whatever you'd like"</FONT>,
                         <FONT color=3D#00cd00>-EXPIRES</FONT> =3D&gt; =
<FONT color=3D#00cd00>"+2y"</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>-COOKIE</FONT> =3D&gt; =
$packed_cookie<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/ic_cookies">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -w
# ic_cookies - sample CGI script that uses a cookie
</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>

<FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>strict</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$cookname</FONT> =
=3D <FONT color=3D#00cd00>"favorite ice cream"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$favorite</FONT> =
=3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"flavor"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$tasty</FONT>    =
=3D cookie<FONT color=3D#00ffff>(</FONT>$cookname<FONT =
color=3D#00ffff>)</FONT> || <FONT color=3D#00cd00>'mint'</FONT><FONT =
color=3D#00ffff>;</FONT>

<FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#00ffff>(</FONT>$favorite<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> header<FONT =
color=3D#00ffff>()</FONT>, start_html<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Ice Cookies"</FONT><FONT color=3D#00ffff>)</FONT>, =
h1<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Hello Ice =
Cream"</FONT><FONT color=3D#00ffff>)</FONT>,
          hr<FONT color=3D#00ffff>()</FONT>, start_form<FONT =
color=3D#00ffff>()</FONT>,
            p<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Please =
select a flavor: "</FONT>, textfield<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"flavor"</FONT>,$tasty<FONT color=3D#00ffff>))</FONT>,
              end_form<FONT color=3D#00ffff>()</FONT>, hr<FONT =
color=3D#00ffff>();</FONT>
    <FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$cookie</FONT> =3D =
cookie<FONT color=3D#00ffff>(</FONT>
                <FONT color=3D#00cd00>-NAME</FONT>    =3D&gt; $cookname,
                <FONT color=3D#00cd00>-VALUE</FONT>   =3D&gt; $favorite,
                <FONT color=3D#00cd00>-EXPIRES</FONT> =3D&gt; <FONT =
color=3D#00cd00>"+2y"</FONT>,
            <FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ff7f50>print</FONT> header<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>-COOKIE</FONT> =3D&gt; =
$cookie<FONT color=3D#00ffff>)</FONT>,
      start_html<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Ice =
Cookies, #2"</FONT><FONT color=3D#00ffff>)</FONT>,
      h1<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Hello Ice =
Cream"</FONT><FONT color=3D#00ffff>)</FONT>,
      p<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"You chose as =
your favorite flavor `$favorite'."</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=3DAEN1040>Creating Sticky Widgets</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#ff7f50>print</FONT> textfield<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"SEARCH"</FONT><FONT =
color=3D#00ffff>);</FONT>          <FONT color=3D#bebebe># previous =
SEARCH value is the default
#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/who.cgi">download =
the following standalone program</A></FONT>
#!/usr/bin/perl -wT
# who.cgi - run who(1) on a user and format the results nicely
</FONT>
<FONT color=3D#cdcd00>$ENV</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>IFS</FONT><FONT color=3D#00ffff>}</FONT>=3D<FONT =
color=3D#00cd00>''</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>$ENV</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>PATH</FONT><FONT color=3D#00ffff>}</FONT>=3D<FONT =
color=3D#00cd00>'/bin:/usr/bin'</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<FONT =
color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe># print search form
</FONT><FONT color=3D#ff7f50>print</FONT> header<FONT =
color=3D#00ffff>()</FONT>, start_html<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Query Users"</FONT><FONT color=3D#00ffff>)</FONT>, =
h1<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Search"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> start_form<FONT =
color=3D#00ffff>()</FONT>, p<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Which user?"</FONT>, textfield<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"WHO"</FONT><FONT =
color=3D#00ffff>));</FONT> submit<FONT color=3D#00ffff>()</FONT>, =
end_form<FONT color=3D#00ffff>();</FONT>

<FONT color=3D#bebebe># print results of the query if we have someone to =
look for
</FONT>$name =3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"WHO"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT>$name<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> h1<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Results"</FONT><FONT =
color=3D#00ffff>);</FONT>
    $html =3D <FONT color=3D#00cd00>''</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#43cd80>    </FONT>
    <FONT color=3D#bebebe># call who and build up text of response
</FONT>    <FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>`who`</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ffa500>next</FONT> <FONT =
color=3D#ffa500>unless</FONT> /^$name\s/o<FONT color=3D#00ffff>;</FONT>  =
          <FONT color=3D#bebebe># only lines matching $name
</FONT>        <FONT color=3D#ff7f50>s</FONT>/&amp;/&amp;amp<FONT =
color=3D#00ffff>;</FONT>/g<FONT color=3D#00ffff>;</FONT>                 =
       <FONT color=3D#bebebe># escape HTML
</FONT>        <FONT color=3D#ff7f50>s</FONT>/&lt;/&amp;lt<FONT =
color=3D#00ffff>;</FONT>/g<FONT color=3D#00ffff>;</FONT>
        <FONT color=3D#ff7f50>s</FONT>/&gt;/&amp;gt<FONT =
color=3D#00ffff>;</FONT>/g<FONT color=3D#00ffff>;</FONT>
        $html .=3D $_<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#bebebe># nice message if we didn't find anyone by =
that name
</FONT>    $html =3D $html || <FONT color=3D#00cd00>"$name is not logged =
in"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#43cd80>    </FONT>
    <FONT color=3D#ff7f50>print</FONT> pre<FONT =
color=3D#00ffff>(</FONT>$html<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ff7f50>print</FONT> end_html<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=3DAEN1043>Writing a Multiscreen 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><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>
<FONT color=3D#ff7f50>print</FONT> hidden<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"bacon"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>    <FONT color=3D#ff7f50>print</FONT> submit<FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>-NAME</FONT> =3D&gt; <FONT =
color=3D#00cd00>".State"</FONT>, <FONT color=3D#00cd00>-VALUE</FONT> =
=3D&gt; <FONT color=3D#00cd00>"Checkout"</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>to_page</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ffa500>return</FONT> submit<FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#00cd00>-NAME</FONT> =3D&gt; <FONT =
color=3D#00cd00>".State"</FONT>, <FONT color=3D#00cd00>-VALUE</FONT> =
=3D&gt; <FONT color=3D#ff7f50>shift</FONT> <FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------

⌨️ 快捷键说明

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