📄 function.header.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Send a raw HTTP header</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><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><hr /><div id="function.header" class="refentry"> <div class="refnamediv"> <h1 class="refname">header</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">header</span> — <span class="dc-title">Send a raw HTTP header</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><b><b>header</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$string</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$replace</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$http_response_code</tt></span> ]] )</div> <p class="para rdfs-comment"> <b>header()</b> is used to send a raw <acronym title="Hypertext Transfer Protocol">HTTP</acronym> header. See the <a href="http://www.faqs.org/rfcs/rfc2616" class="link external">» HTTP/1.1 specification</a> for more information on <acronym title="Hypertext Transfer Protocol">HTTP</acronym> headers. </p> <p class="para"> Remember that <b>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 <b>include()</b>, or <b>require()</b>, functions, or another file access function, and have spaces or empty lines that are output before <b>header()</b> is called. The same problem exists when using a single PHP/HTML file. <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><html><br /><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* This will give an error. Note the output<br /> * above, which is before the header() call */<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location: http://www.example.com/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">string</tt></i></span> <dd> <p class="para"> The header string. </p> <p class="para"> There are two special-case header calls. The first is a header that starts with the string "<i>HTTP/</i>" (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 <i>ErrorDocument</i> directive), you may want to make sure that your script generates the proper status code. </p> <p class="para"> <div class="informalexample"> <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">"HTTP/1.0 404 Not Found"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a <i>REDIRECT</i> (302) status code to the browser unless some <i>3xx</i> status code has already been set. </p> <p class="para"> <div class="informalexample"> <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">"Location: http://www.example.com/"</span><span style="color: #007700">); </span><span style="color: #FF8000">/* Redirect browser */<br /><br />/* Make sure that code below does not get executed when we redirect. */<br /></span><span style="color: #007700">exit;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">replace</tt></i></span> <dd> <p class="para"> The optional <i><tt class="parameter">replace</tt></i> 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 <b><tt>FALSE</tt></b> as the second argument you can force multiple headers of the same type. For example: </p> <p class="para"> <div class="informalexample"> <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">'WWW-Authenticate: Negotiate'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'WWW-Authenticate: NTLM'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">http_response_code</tt></i></span> <dd> <p class="para"> Forces the HTTP response code to the specified value. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -