<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Send a raw HTTP header</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.header-remove.html">header_remove</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" id="refsect1-function.header-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><span class="type void">void</span></span> <span class="methodname"><strong>header</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$replace</code><span class="initializer"> = true</span></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$http_response_code</code></span> ]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>header()</strong></span> 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 <span class="function"><strong>header()</strong></span> 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 <span class="function"><a href="function.include.html" class="function">include</a></span>, or <span class="function"><a href="function.require.html" class="function">require</a></span>, functions, or another file access function, and have spaces or empty lines that are output before <span class="function"><strong>header()</strong></span> 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 />exit;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 parameters" id="refsect1-function.header-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">string</code></em></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 "<em>HTTP/</em>" (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 <em>ErrorDocument</em> 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 <em>REDIRECT</em> (302) status code to the browser unless the <em>201</em> or a <em>3xx</em> 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"><em><code class="parameter">replace</code></em></span> <dd> <p class="para"> The optional <em><code class="parameter">replace</code></em> 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 <strong><code>FALSE</code></strong> 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"><em><code class="parameter">http_response_code</code></em></span> <dd> <p class="para"> Forces the HTTP response code to the specified value. Note that this parameter only has an effect if the <em><code class="parameter">string</code></em> is not empty. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.header-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.header-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.4.2 and 5.1.2</td> <td> This function now prevents more than one header to be sent at once as a protection against header injection attacks. </td> </tr> <tr> <td>4.3.0</td> <td> The <em><code class="parameter">http_response_code</code></em> parameter was added. </td> </tr> <tr> <td>4.0.4</td> <td> The <em><code class="parameter">replace</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.header-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4484"> <p><strong>Example #1 Download dialog</strong></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" id="example-4485"> <p><strong>Example #2 Caching directives</strong></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 class="note"><p><strong class="note">Note</strong>: <p class="para"> 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 class="para"> Additionally, <span class="function"><a href="function.session-cache-limiter.html" class="function">session_cache_limiter()</a></span> and the <em>session.cache_limiter</em> configuration setting can be used to automatically generate the correct caching-related headers when sessions are being used. </p> </p></blockquote> </p></div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.header-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Headers will only be accessible and output when a SAPI that supports them is in use. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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 <span class="function"><a href="function.ob-start.html" class="function">ob_start()</a></span> and <span class="function"><a href="function.ob-end-flush.html" class="function">ob_end_flush()</a></span> in your script, or setting the <em>output_buffering</em> configuration directive on in your <var class="filename">php.ini</var> or server configuration files. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The HTTP status header line will always be the first sent to the client, regardless of the actual <span class="function"><strong>header()</strong></span> call being the first or not. The status may be overridden by calling <span class="function"><strong>header()</strong></span> with a new status line at any time unless the HTTP headers have already been sent. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> If <a href="ini.sect.safe-mode.html#ini.safe-mode" class="link">safe mode</a> is enabled the uid of the script is added to the <em>realm</em> part of the <em>WWW-Authenticate</em> header if you set this header (used for HTTP Authentication). </span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['HTTP_HOST']</a></var></var>, <var class="varname"><var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['PHP_SELF']</a></var></var> and <span class="function"><a href="function.dirname.html" class="function">dirname()</a></span> 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://</span><span style="color: #0000BB">$host$uri</span><span style="color: #DD0000">/</span><span style="color: #0000BB">$extra</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />exit;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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 <strong><code>SID</code></strong> constant. </p> </p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.header-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.headers-sent.html" class="function" rel="rdfs-seeAlso">headers_sent()</a> - Checks if or where headers have been sent</span></li> <li class="member"> <span class="function"><a href="function.setcookie.html" class="function" rel="rdfs-seeAlso">setcookie()</a> - Send a cookie</span></li> <li class="member"> <span class="function"><a href="function.http-response-code.html" class="function" rel="rdfs-seeAlso">http_response_code()</a> - Get or Set the HTTP response code</span></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 class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.header-remove.html">header_remove</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>