📄 function.header.html
字号:
<tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.4.2 and 5.1.2</td> <td colspan="1" rowspan="1" align="left"> This function now prevents more than one header to be sent at once as a protection against header injection attacks. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">http_response_code</tt></i> parameter was added. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.0.4</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">replace</tt></i> parameter was added. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Download dialog</b></p> <div class="example-contents"><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" class="link external">» Content-Disposition</a> header to supply a recommended filename and force the browser to display the save dialog. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// We'll be outputting a PDF<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-type: application/pdf'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// It will be called downloaded.pdf<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Disposition: attachment; filename="downloaded.pdf"'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The PDF source is in original.pdf<br /></span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #DD0000">'original.pdf'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Caching directives</b></p> <div class="example-contents"><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></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Cache-Control: no-cache, must-revalidate"</span><span style="color: #007700">); </span><span style="color: #FF8000">// HTTP/1.1<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Expires: Sat, 26 Jul 1997 05:00:00 GMT"</span><span style="color: #007700">); </span><span style="color: #FF8000">// Date in the past<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p> <blockquote><p><b class="note">Note</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. <br /> Additionally, <a href="function.session-cache-limiter.html" class="function">session_cache_limiter()</a> and the <i>session.cache_limiter</i> configuration setting can be used to automatically generate the correct caching-related headers when sessions are being used. <br /> </p></blockquote> </p></div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</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" class="function">ob_start()</a> and <a href="function.ob-end-flush.html" class="function">ob_end_flush()</a> in your script, or setting the <i>output_buffering</i> configuration directive on in your <var class="filename">php.ini</var> or server configuration files. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: The HTTP status header line will always be the first sent to the client, regardless of the actual <b>header()</b> call being the first or not. The status may be overridden by calling <b>header()</b> with a new status line at any time unless the HTTP headers have already been sent. <br /> </p></blockquote> <blockquote><p><b class="note">Note</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. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> If <a href="features.safe-mode.html#ini.safe-mode" class="link">safe mode</a> is enabled the uid of the script is added to the <i>realm</i> part of the <i>WWW-Authenticate</i> header if you set this header (used for HTTP Authentication). </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: HTTP/1.1 requires an absolute <acronym title="Uniform Resource Identifier">URI</acronym> as argument to <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30" class="link external">» Location:</a> including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['HTTP_HOST']</a></var>, <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['PHP_SELF']</a></var> and <a href="function.dirname.html" class="function">dirname()</a> to make an absolute URI from a relative one yourself: <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Redirect to a different page in the current directory that was requested */<br /></span><span style="color: #0000BB">$host </span><span style="color: #007700">= </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_HOST'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$uri </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_SELF'</span><span style="color: #007700">]), </span><span style="color: #DD0000">'/\\'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$extra </span><span style="color: #007700">= </span><span style="color: #DD0000">'mypage.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Location: http://$host$uri/$extra"</span><span style="color: #007700">);<br />exit;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: Session ID is not passed with Location header even if <a href="session.configuration.html#ini.session.use-trans-sid" class="link">session.use_trans_sid</a> is enabled. It must by passed manually using <b><tt>SID</tt></b> constant. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.headers-sent.html" class="function" rel="rdfs-seeAlso">headers_sent()</a></li> <li class="member"><a href="function.setcookie.html" class="function" rel="rdfs-seeAlso">setcookie()</a></li> <li class="member"> The section on <a href="features.http-auth.html" class="link">HTTP authentication</a> </li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.getservbyport.html">getservbyport</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.headers-list.html">headers_list</a></div> <div class="up"><a href="ref.network.html">Network Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -