<HTML ><HEAD ><TITLE >header</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="HTTP functions" HREF="ref.http.html"><LINK REL="PREVIOUS" TITLE="HTTP functions" HREF="ref.http.html"><LINK REL="NEXT" TITLE="headers_sent" HREF="function.headers-sent.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></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 Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="ref.http.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.headers-sent.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.header" ></A >header</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN30461" ></A ><P > (PHP 3, PHP 4 )</P >header -- Send a raw HTTP header</DIV ><DIV CLASS="refsect1" ><A NAME="AEN30464" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >header</B > ( string string [, bool replace [, int http_reponse_code]])<BR ></BR ><P > <B CLASS="function" >header()</B > is used to send raw <SPAN CLASS="acronym" >HTTP</SPAN > headers. See the <A HREF="http://www.w3.org/Protocols/rfc2616/rfc2616" TARGET="_top" >HTTP/1.1 specification</A > for more information on <SPAN CLASS="acronym" >HTTP</SPAN > headers. </P ><P > The optional <TT CLASS="parameter" ><I >replace</I ></TT > 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: <DIV CLASS="informalexample" ><A NAME="AEN30486" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php header('WWW-Authenticate: Negotiate'); header('WWW-Authenticate: NTLM', FALSE); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The second optional <TT CLASS="parameter" ><I >http_response_code</I ></TT > 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. <DIV CLASS="informalexample" ><A NAME="AEN30493" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php header("HTTP/1.0 404 Not Found"); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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 beeing 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 > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > In PHP 3, this only works when PHP is compiled as an Apache module. You can achieve the same effect using the <TT CLASS="literal" >Status</TT > header. <DIV CLASS="informalexample" ><A NAME="AEN30502" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php header("Status: 404 Not Found"); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV > </P ><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. <DIV CLASS="informalexample" ><A NAME="AEN30507" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php header("Location: http://www.example.com/"); /* Redirect browser */ exit; /* Make sure that code below does not get executed when we redirect. */ ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > HTTP/1.1 requires an absolute <SPAN CLASS="acronym" >URI</SPAN > 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" ><A NAME="AEN30516" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php header("Location: http://".$_SERVER['HTTP_HOST'] .dirname($_SERVER['PHP_SELF']) ."/".$relative_url); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </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 <DIV CLASS="informalexample" ><A NAME="AEN30519" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1 header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // HTTP/1.0 ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >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. </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 ><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. <DIV CLASS="informalexample" ><A NAME="AEN30531" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><html> <?php // This will give an error. Note the output // above, which is before the header() call header('Location: http://www.example.com/'); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > In 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 ><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.ietf.org/rfc/rfc2183.txt" TARGET="_top" >Content-Disposition</A > header to supply a recommended filename and force the browser to display the save dialog. <DIV CLASS="informalexample" ><A NAME="AEN30541" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php // We'll be outputting a PDF header("Content-type: application/pdf"); // It will be called downloaded.pdf header("Content-Disposition: attachment; filename=downloaded.pdf"); // The PDF source is in original.pdf readfile('original.pdf'); ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >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. </P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </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="ref.http.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.headers-sent.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >HTTP functions</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.http.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >headers_sent</TD ></TR ></TABLE ></DIV ></BODY ></HTML >