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

📄 ch19_09.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 19.8. Redirecting to a Different Location (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:41Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_08.htm"TITLE="19.7. Formatting Lists and Tables with HTML Shortcuts"><LINKREL="next"HREF="ch19_10.htm"TITLE="19.9. Debugging the Raw HTTP Exchange"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_08.htm"TITLE="19.7. Formatting Lists and Tables with HTML Shortcuts"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.7. Formatting Lists and Tables with HTML Shortcuts"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_10.htm"TITLE="19.9. Debugging the Raw HTTP Exchange"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.9. Debugging the Raw HTTP Exchange"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-chap19_redirecting_1">19.8. Redirecting to a Different Location</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-818">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005466-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005466-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005466-2"></A></A></H3><PCLASS="para">You need to tell the client's browser to look elsewhere for a page.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-824">Solution</A></H3><PCLASS="para">Instead of a normal header, just issue a location redirect and exit. Don't forget the extra newline at the end of the header.</P><PRECLASS="programlisting">$url = &quot;http://www.perl.com/CPAN/&quot;;print &quot;Location: $url\n\n&quot;;exit;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-836">Discussion</A></H3><PCLASS="para">Sometimes your CGI program doesn't need to generate the document on its own. It only needs to tell the client at the other end to fetch a different document instead. In that case, the HTTP header needs to include this directive as a <CODECLASS="literal">Location</CODE> line followed by the URL you want to send them to. Make sure to use an absolute URL, not a relative one.</P><PCLASS="para">The direct and literal solution given above is usually sufficient. But if you already have the CGI module loaded, use the <CODECLASS="literal">redirect</CODE><ACLASS="indexterm"NAME="ch19-idx-1000005481-0"></A> function. You might use this code if you are building and setting a cookie, as shown in <ACLASS="xref"HREF="ch19_09.htm#ch19-38561"TITLE="oreobounce">Example 19.4</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch19-38561">Example 19.4: oreobounce</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# <ACLASS="indexterm"NAME="ch19-idx-1000005473-0"></A>oreobounce - set a cookie and redirect the browseruse CGI qw(:cgi);$oreo = cookie( -NAME    =&gt; 'filling',                -VALUE   =&gt; &quot;vanilla cr鑝e&quot;,                -EXPIRES =&gt; '+3M',    # M for month, m for minute                -DOMAIN  =&gt; '.perl.com');$whither  = &quot;http://somewhere.perl.com/nonesuch.html&quot;;print redirect( -URL     =&gt; $whither,                -COOKIE  =&gt; $oreo);</PRE></DIV><PCLASS="para">That would produce:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Status: 302 Moved Temporarily</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Set-Cookie: filling=vanilla%20cr%E4me; domain=.perl.com; </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    expires=Tue, 21-Jul-1998 11:58:55 GMT</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Date: Tue, 21 Apr 1998 11:55:55 GMT</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Location: http://somewhere.perl.com/nonesuch.html</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Content-Type: text/html</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>B&lt;&lt;blank line here&gt;&gt;</I></CODE></B></CODE></PRE><PCLASS="para"><ACLASS="xref"HREF="ch19_09.htm#ch19-33622"TITLE="os_snipe">Example 19.5</A> is a complete program that looks at the client browser name and redirects it to a page in Eric Raymond's <EMCLASS="emphasis">Jargon File</EM> that talks about the user's browser. It's also a nice example of a different approach to building a <ACLASS="indexterm"NAME="ch19-idx-1000005593-0"></A>switch statement in Perl.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch19-33622">Example 19.5: os_snipe</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# <ACLASS="indexterm"NAME="ch19-idx-1000005472-0"></A>os_snipe - redirect to a Jargon File entry about current OS$dir = 'http://www.wins.uva.nl/%7Emes/jargon';for ($ENV{HTTP_USER_AGENT}) {    $page  =    /Mac/            &amp;&amp; 'm/Macintrash.html'             || /Win(dows )?NT/  &amp;&amp; 'e/evilandrude.html'             || /Win|MSIE|WebTV/ &amp;&amp; 'm/MicroslothWindows.html'             || /Linux/          &amp;&amp; 'l/Linux.html'             || /HP-UX/          &amp;&amp; 'h/HP-SUX.html'             || /SunOS/          &amp;&amp; 's/ScumOS.html'             ||                     'a/AppendixB.html';}print &quot;Location: $dir/$page\n\n&quot;;</PRE></DIV><PCLASS="para">The <EMCLASS="emphasis">os_snipe</EM> program shows a good use of dynamic redirection, because you don't always send every user to the same place. If you did, it would usually make more sense to arrange for a static redirect line in the server's configuration file, since that would be easier on the web server than running a CGI script for each redirection.</P><PCLASS="para">Telling the client's browser that you don't plan to produce any output is not the same as redirecting nowhere:</P><PRECLASS="programlisting">use CGI qw(:standard);print header( -STATUS =&gt; '204 No response' );</PRE><PCLASS="para">That produces this:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Status: 204 No response</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Content-Type: text/html</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;blank line here&gt;</I></CODE></B></CODE></PRE><PCLASS="para">Use this, for instance, when the user will submit a form request but you don't want their page to change or even update.</P><PCLASS="para">It may seem silly to provide a content type and then no content, but that's what the module does. If you were hand-coding this, it wouldn't be required.</P><PRECLASS="programlisting">#!/bin/shcat &lt;&lt;EOCATStatus: 204 No response EOCAT<ACLASS="indexterm"NAME="ch19-idx-1000005468-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005468-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005468-2"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-944">See Also</A></H3><PCLASS="para">The documentation for the standard CGI module</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_08.htm"TITLE="19.7. Formatting Lists and Tables with HTML Shortcuts"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.7. Formatting Lists and Tables with HTML Shortcuts"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_10.htm"TITLE="19.9. Debugging the Raw HTTP Exchange"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.9. Debugging the Raw HTTP Exchange"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.7. Formatting Lists and Tables with HTML Shortcuts</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.9. Debugging the Raw HTTP Exchange</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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