📄 cgiprogramming.mht
字号:
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-><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->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 > $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->errstr<FONT =
color=3D#00ffff>;</FONT>
$sth->execute
<FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Executing: "</FONT>, $sth->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>"<TABLE =
BORDER=3D1>"</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->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>"</TABLE>\n"</FONT><FONT color=3D#00ffff>;</FONT>
$sth->finish<FONT color=3D#00ffff>;</FONT>
$dbh->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> <FONT =
color=3D#00cd00>'filling'</FONT>,
<FONT color=3D#00cd00>-VALUE</FONT> =3D> <FONT =
color=3D#00cd00>"vanilla cr=E8me"</FONT>,
<FONT color=3D#00cd00>-EXPIRES</FONT> =3D> <FONT =
color=3D#00cd00>'+3M'</FONT>, <FONT color=3D#bebebe># M for month, m =
for minute
</FONT> <FONT color=3D#00cd00>-DOMAIN</FONT> =3D> =
<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> =
$whither,
<FONT color=3D#00cd00>-COOKIE</FONT> =3D> $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<<blank line here>>
#-----------------------------
# <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/ && <FONT =
color=3D#00cd00>'m/Macintrash.html'</FONT>
|| /Win<FONT color=3D#00ffff>(</FONT>dows <FONT =
color=3D#00ffff>)</FONT>?NT/ && <FONT =
color=3D#00cd00>'e/evilandrude.html'</FONT>
|| /Win|MSIE|WebTV/ && <FONT =
color=3D#00cd00>'m/MicroslothWindows.html'</FONT>
|| /Linux/ && <FONT =
color=3D#00cd00>'l/Linux.html'</FONT>
|| /HP-UX/ && <FONT =
color=3D#00cd00>'h/HP-SUX.html'</FONT>
|| /SunOS/ && <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> <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
#
#<blank line here>
#-----------------------------
#!/bin/sh
</FONT>
cat <<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->new<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>Timeout</FONT> =3D> <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: <URL:"</FONT>, $server->url, <FONT =
color=3D#00cd00>">\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-><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->get_request<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> $answer->as_string<FONT =
color=3D#00ffff>;</FONT>
$client->autoflush<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>RESPONSE:</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><STDIN><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->reason, <FONT color=3D#00cd00>"\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
$client-><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 + -