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

📄 cgiprogramming.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
color=3D#00ffff>{</FONT><FONT color=3D#cdcd00>$hash</FONT><FONT =
color=3D#00ffff>{</FONT>$_<FONT color=3D#00ffff>}}</FONT> <FONT =
color=3D#00ffff>]</FONT> <FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>}</FONT> <FONT color=3D#ff7f50>sort</FONT> <FONT =
color=3D#ff7f50>keys</FONT> <FONT color=3D#cdcd00>%hash</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/salcheck">download=
 the following standalone program</A></FONT>
#!/usr/bin/perl
# salcheck - check for salaries
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>DBI</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 =
:html3<FONT color=3D#00ffff>);</FONT>

$limit =3D param<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"LIMIT"</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>"Salary Query"</FONT><FONT color=3D#00ffff>)</FONT>,
      h1<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Search"</FONT><FONT color=3D#00ffff>)</FONT>,
      start_form<FONT color=3D#00ffff>()</FONT>,
      p<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Enter =
minimum salary"</FONT>, textfield<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"LIMIT"</FONT><FONT color=3D#00ffff>))</FONT>,
      submit<FONT color=3D#00ffff>()</FONT>,
      end_form<FONT color=3D#00ffff>();</FONT>

<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>defined</FONT> $limit<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
    $dbh =3D DBI-&gt;<FONT color=3D#98fb98>connect</FONT><FONT =
color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"dbi:mysql:somedb:server.host.dom:3306"</FONT>,<FONT =
color=3D#43cd80> </FONT>
        <FONT color=3D#00cd00>"username"</FONT>, <FONT =
color=3D#00cd00>"password"</FONT><FONT color=3D#00ffff>)</FONT><FONT =
color=3D#43cd80> </FONT>
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Connecting: $DBI::errstr"</FONT><FONT =
color=3D#00ffff>;</FONT>
    $sth =3D $dbh-&gt;prepare<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"SELECT name,salary FROM employees</FONT><FONT =
color=3D#43cd80> </FONT><FONT color=3D#00cd00>
        WHERE salary &gt; $limit"</FONT><FONT color=3D#00ffff>)</FONT>
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Preparing: "</FONT>, $dbh-&gt;errstr<FONT =
color=3D#00ffff>;</FONT>
    $sth-&gt;execute
        <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Executing: "</FONT>, $sth-&gt;errstr<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>, <FONT color=3D#00cd00>"&lt;TABLE =
BORDER=3D1&gt;"</FONT><FONT color=3D#00ffff>;</FONT>

    <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdad00>@row</FONT> =3D =
$sth-&gt;fetchrow<FONT color=3D#00ffff>())</FONT> <FONT =
color=3D#00ffff>{</FONT>
           <FONT color=3D#ff7f50>print</FONT> Tr<FONT =
color=3D#00ffff>(</FONT> td<FONT color=3D#00ffff>(</FONT> \<FONT =
color=3D#cdad00>@row</FONT> <FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>);</FONT>
    <FONT color=3D#00ffff>}</FONT>

    <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"&lt;/TABLE&gt;\n"</FONT><FONT color=3D#00ffff>;</FONT>
    $sth-&gt;finish<FONT color=3D#00ffff>;</FONT>
    $dbh-&gt;disconnect<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=3DAEN1031>Redirecting to a Different =
Location</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>$url =3D <FONT =
color=3D#00cd00>"http://www.perl.com/CPAN/"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Location: =
$url\n\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>exit</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/oreobounce">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -w
# oreobounce - set a cookie and redirect the browser
</FONT><FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>CGI</FONT> =
<FONT color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>:cgi<FONT =
color=3D#00ffff>);</FONT>

$oreo =3D cookie<FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#00cd00>-NAME</FONT>    =3D&gt; <FONT =
color=3D#00cd00>'filling'</FONT>,
                <FONT color=3D#00cd00>-VALUE</FONT>   =3D&gt; <FONT =
color=3D#00cd00>"vanilla cr=E8me"</FONT>,
                <FONT color=3D#00cd00>-EXPIRES</FONT> =3D&gt; <FONT =
color=3D#00cd00>'+3M'</FONT>,    <FONT color=3D#bebebe># M for month, m =
for minute
</FONT>                <FONT color=3D#00cd00>-DOMAIN</FONT>  =3D&gt; =
<FONT color=3D#00cd00>'.perl.com'</FONT><FONT color=3D#00ffff>);</FONT>

$whither  =3D <FONT =
color=3D#00cd00>"http://somewhere.perl.com/nonesuch.html"</FONT><FONT =
color=3D#00ffff>;</FONT>

<FONT color=3D#ff7f50>print</FONT> redirect<FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#00cd00>-URL</FONT>     =3D&gt; =
$whither,
                <FONT color=3D#00cd00>-COOKIE</FONT>  =3D&gt; $oreo<FONT =
color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe>#-----------------------------
#Status: 302 Moved Temporarily
#
#Set-Cookie: filling=3Dvanilla%20cr</FONT><FONT =
color=3D#cdcd00>%E4me</FONT><FONT color=3D#bebebe>; =
domain=3D.perl.com;</FONT><FONT color=3D#43cd80> </FONT><FONT =
color=3D#bebebe>
#
#    expires=3DTue, 21-Jul-1998 11:58:55 GMT
#
#Date: Tue, 21 Apr 1998 11:55:55 GMT
#
#Location: http://somewhere.perl.com/nonesuch.html
#
#Content-Type: text/html
#
#B&lt;&lt;blank line here&gt;&gt;
#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch19/os_snipe">download=
 the following standalone program</A></FONT>
#!/usr/bin/perl
# os_snipe - redirect to a Jargon File entry about current OS
</FONT>$dir =3D <FONT =
color=3D#00cd00>'http://www.wins.uva.nl/%7Emes/jargon'</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>$ENV</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>HTTP_USER_AGENT</FONT><FONT color=3D#00ffff>})</FONT> =
<FONT color=3D#00ffff>{</FONT>
    $page  =3D    /Mac/            &amp;&amp; <FONT =
color=3D#00cd00>'m/Macintrash.html'</FONT>
             || /Win<FONT color=3D#00ffff>(</FONT>dows <FONT =
color=3D#00ffff>)</FONT>?NT/  &amp;&amp; <FONT =
color=3D#00cd00>'e/evilandrude.html'</FONT>
             || /Win|MSIE|WebTV/ &amp;&amp; <FONT =
color=3D#00cd00>'m/MicroslothWindows.html'</FONT>
             || /Linux/          &amp;&amp; <FONT =
color=3D#00cd00>'l/Linux.html'</FONT>
             || /HP-UX/          &amp;&amp; <FONT =
color=3D#00cd00>'h/HP-SUX.html'</FONT>
             || /SunOS/          &amp;&amp; <FONT =
color=3D#00cd00>'s/ScumOS.html'</FONT>
             ||                     <FONT =
color=3D#00cd00>'a/AppendixB.html'</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Location: =
$dir/$page\n\n"</FONT><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>
<FONT color=3D#ff7f50>print</FONT> header<FONT color=3D#00ffff>(</FONT> =
<FONT color=3D#00cd00>-STATUS</FONT> =3D&gt; <FONT color=3D#00cd00>'204 =
No response'</FONT> <FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
#Status: 204 No response
#
#Content-Type: text/html
#
#&lt;blank line here&gt;
#-----------------------------
#!/bin/sh
</FONT>
cat &lt;&lt;EOCAT
Status: <FONT color=3D#cdcd00>204</FONT> No response
<FONT color=3D#43cd80> </FONT>
EOCAT
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN1034>Debugging the Raw HTTP =
Exchange</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/dummyhttpd">downlo=
ad the following standalone program</A></FONT>
#!/usr/bin/perl -w
# dummyhttpd - start an HTTP daemon and print what the client sends
</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>HTTP::Daemon</FONT><FONT color=3D#00ffff>;</FONT>  <FONT =
color=3D#bebebe># need LWP-5.32 or better
</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$server</FONT> =3D =
HTTP::Daemon-&gt;new<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>Timeout</FONT> =3D&gt; <FONT =
color=3D#cdcd00>60</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Please contact =
me at: &lt;URL:"</FONT>, $server-&gt;url, <FONT =
color=3D#00cd00>"&gt;\n"</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ffa500>my</FONT> $client =3D $server-&gt;<FONT =
color=3D#98fb98>accept</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
  <FONT color=3D#ff7f50>CONNECTION:</FONT>
    <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ffa500>my</FONT> $answer =3D =
$client-&gt;get_request<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT> $answer-&gt;as_string<FONT =
color=3D#00ffff>;</FONT>
        $client-&gt;autoflush<FONT color=3D#00ffff>;</FONT>
      <FONT color=3D#ff7f50>RESPONSE:</FONT>
        <FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT>&lt;STDIN&gt;<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#00ffff>{</FONT>
            <FONT color=3D#ffa500>last</FONT> <FONT =
color=3D#ff7f50>RESPONSE</FONT>   <FONT color=3D#ffa500>if</FONT> $_ =
<FONT color=3D#98fb98>eq</FONT> <FONT color=3D#00cd00>".\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
            <FONT color=3D#ffa500>last</FONT> <FONT =
color=3D#ff7f50>CONNECTION</FONT> <FONT color=3D#ffa500>if</FONT> $_ =
<FONT color=3D#98fb98>eq</FONT> <FONT color=3D#00cd00>"..\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
            <FONT color=3D#ff7f50>print</FONT> $client $_<FONT =
color=3D#00ffff>;</FONT>
        <FONT color=3D#00ffff>}</FONT>
        <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"\nEOF\n"</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"CLOSE: =
"</FONT>, $client-&gt;reason, <FONT color=3D#00cd00>"\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
    $client-&gt;<FONT color=3D#98fb98>close</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ff7f50>undef</FONT> $client<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe>#-----------------------------
#http://somewhere.com/cgi-bin/whatever
#-----------------------------

⌨️ 快捷键说明

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