<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >header</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Network Functions" HREF="ref.network.html"><LINK REL="PREVIOUS" TITLE="getservbyport" HREF="function.getservbyport.html"><LINK REL="NEXT" TITLE="headers_list" HREF="function.headers-list.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.getservbyport.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.headers-list.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.header" ></A >header</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN149312" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >header -- Send a raw HTTP header</DIV ><DIV CLASS="refsect1" ><A NAME="AEN149315" ></A ><H2 >Description</H2 >void <B CLASS="methodname" >header</B > ( string string [, bool replace [, int http_response_code]] )<BR ></BR ><P > <B CLASS="function" >header()</B > is used to send a raw <ACRONYM CLASS="acronym" >HTTP</ACRONYM > header. See the <A HREF="http://www.faqs.org/rfcs/rfc2616" TARGET="_top" >HTTP/1.1 specification</A > for more information on <ACRONYM CLASS="acronym" >HTTP</ACRONYM > headers. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > Since PHP 4.4.2 and PHP 5.1.2 this function prevents more than one header to be sent at once as a protection against header injection attacks. </P ></BLOCKQUOTE ></DIV ><P > The optional <CODE CLASS="parameter" >replace</CODE > parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in <TT CLASS="constant" ><B >FALSE</B ></TT > as the second argument you can force multiple headers of the same type. For example: </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149340" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />header</font><font color="#007700">(</font><font color="#DD0000">'WWW-Authenticate: Negotiate'</font><font color="#007700">);<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">'WWW-Authenticate: NTLM'</font><font color="#007700">, </font><font color="#0000BB">false</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The second optional <CODE CLASS="parameter" >http_response_code</CODE > force the HTTP response code to the specified value. (This parameter is available in PHP 4.3.0 and higher.) </P ><P > There are two special-case header calls. The first is a header that starts with the string "<TT CLASS="literal" >HTTP/</TT >" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the <TT CLASS="literal" >ErrorDocument</TT > directive), you may want to make sure that your script generates the proper status code. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149348" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />header</font><font color="#007700">(</font><font color="#DD0000">"HTTP/1.0 404 Not Found"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The HTTP status header line will always be the first sent to the client, regardless of the actual <B CLASS="function" >header()</B > call being the first or not. The status may be overridden by calling <B CLASS="function" >header()</B > with a new status line at any time unless the HTTP headers have already been sent. </P ></BLOCKQUOTE ></DIV ><P > The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a <TT CLASS="literal" >REDIRECT</TT > (302) status code to the browser unless some <TT CLASS="literal" >3xx</TT > status code has already been set. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149358" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />header</font><font color="#007700">(</font><font color="#DD0000">"Location: http://www.example.com/"</font><font color="#007700">); </font><font color="#FF8000">/* Redirect browser */<br /><br />/* Make sure that code below does not get executed when we redirect. */<br /></font><font color="#007700">exit;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > HTTP/1.1 requires an absolute <ACRONYM CLASS="acronym" >URI</ACRONYM > as argument to <A HREF="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30" TARGET="_top" >Location:</A > including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use <TT CLASS="literal" >$_SERVER['HTTP_HOST']</TT >, <TT CLASS="literal" >$_SERVER['PHP_SELF']</TT > and <A HREF="function.dirname.html" ><B CLASS="function" >dirname()</B ></A > to make an absolute URI from a relative one yourself: <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149367" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* Redirect to a different page in the current directory that was requested */<br /></font><font color="#0000BB">$host </font><font color="#007700">= </font><font color="#0000BB">$_SERVER</font><font color="#007700">[</font><font color="#DD0000">'HTTP_HOST'</font><font color="#007700">];<br /></font><font color="#0000BB">$uri </font><font color="#007700">= </font><font color="#0000BB">rtrim</font><font color="#007700">(</font><font color="#0000BB">dirname</font><font color="#007700">(</font><font color="#0000BB">$_SERVER</font><font color="#007700">[</font><font color="#DD0000">'PHP_SELF'</font><font color="#007700">]), </font><font color="#DD0000">'/\\'</font><font color="#007700">);<br /></font><font color="#0000BB">$extra </font><font color="#007700">= </font><font color="#DD0000">'mypage.php'</font><font color="#007700">;<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">"Location: http://$host$uri/$extra"</font><font color="#007700">);<br />exit;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > Session ID is not passed with Location header even if <A HREF="ref.session.html#ini.session.use-trans-sid" >session.use_trans_sid</A > is enabled. It must by passed manually using <TT CLASS="constant" ><B >SID</B ></TT > constant. </P ></BLOCKQUOTE ></DIV ><P > PHP scripts often generate dynamic content that must not be cached by the client browser or any proxy caches between the server and the client browser. Many proxies and clients can be forced to disable caching with: </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149375" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />header</font><font color="#007700">(</font><font color="#DD0000">"Cache-Control: no-cache, must-revalidate"</font><font color="#007700">); </font><font color="#FF8000">// HTTP/1.1<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">"Expires: Mon, 26 Jul 1997 05:00:00 GMT"</font><font color="#007700">); </font><font color="#FF8000">// Date in the past<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > You may find that your pages aren't cached even if you don't output all of the headers above. There are a number of options that users may be able to set for their browser that change its default caching behavior. By sending the headers above, you should override any settings that may otherwise cause the output of your script to be cached. </P ><P > Additionally, <A HREF="function.session-cache-limiter.html" ><B CLASS="function" >session_cache_limiter()</B ></A > and the <TT CLASS="literal" >session.cache_limiter</TT > configuration setting can be used to automatically generate the correct caching-related headers when sessions are being used. </P ></BLOCKQUOTE ></DIV ><P > Remember that <B CLASS="function" >header()</B > must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with <A HREF="function.include.html" ><B CLASS="function" >include()</B ></A >, or <A HREF="function.require.html" ><B CLASS="function" >require()</B ></A >, functions, or another file access function, and have spaces or empty lines that are output before <B CLASS="function" >header()</B > is called. The same problem exists when using a single PHP/HTML file. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149388" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <html><br /><font color="#0000BB"><?php<br /></font><font color="#FF8000">/* This will give an error. Note the output<br /> * above, which is before the header() call */<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">'Location: http://www.example.com/'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > As of PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling <A HREF="function.ob-start.html" ><B CLASS="function" >ob_start()</B ></A > and <A HREF="function.ob-end-flush.html" ><B CLASS="function" >ob_end_flush()</B ></A > in your script, or setting the <TT CLASS="literal" >output_buffering</TT > configuration directive on in your <TT CLASS="filename" >php.ini</TT > or server configuration files. </P ></BLOCKQUOTE ></DIV ><P > If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the <A HREF="http://www.faqs.org/rfcs/rfc2183" TARGET="_top" >Content-Disposition</A > header to supply a recommended filename and force the browser to display the save dialog. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN149399" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// We'll be outputting a PDF<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">'Content-type: application/pdf'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// It will be called downloaded.pdf<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">'Content-Disposition: attachment; filename="downloaded.pdf"'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// The PDF source is in original.pdf<br /></font><font color="#0000BB">readfile</font><font color="#007700">(</font><font color="#DD0000">'original.pdf'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > There is a bug in Microsoft Internet Explorer 4.01 that prevents this from working. There is no workaround. There is also a bug in Microsoft Internet Explorer 5.5 that interferes with this, which can be resolved by upgrading to Service Pack 2 or later. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > If <A HREF="features.safe-mode.html#ini.safe-mode" >safe mode</A > is enabled the uid of the script is added to the <TT CLASS="literal" >realm</TT > part of the <TT CLASS="literal" >WWW-Authenticate</TT > header if you set this header (used for HTTP Authentication). </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.headers-sent.html" ><B CLASS="function" >headers_sent()</B ></A >, <A HREF="function.setcookie.html" ><B CLASS="function" >setcookie()</B ></A >, and the section on <A HREF="features.http-auth.html" >HTTP authentication</A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.getservbyport.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.headers-list.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >getservbyport</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.network.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >headers_list</TD ></TR ></TABLE ></DIV ></BODY ></HTML >