<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >header</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" 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_list" HREF="function.headers-list.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="ref.http.html" ACCESSKEY="P" >Înapoi</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" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.header" ></A >header</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN33852" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >header -- Send a raw HTTP header</DIV ><DIV CLASS="refsect1" ><A NAME="AEN33855" ></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 raw <ACRONYM CLASS="acronym" >HTTP</ACRONYM > headers. 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 ><P > The optional <VAR CLASS="parameter" >replace</VAR > 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="AEN33878" ></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 <VAR CLASS="parameter" >http_response_code</VAR > 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 "<VAR CLASS="literal" >HTTP/</VAR >" (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 <VAR CLASS="literal" >ErrorDocument</VAR > directive), you may want to make sure that your script generates the proper status code. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN33886" ></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 >Notã: </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 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > In PHP 3, this only works when PHP is compiled as an Apache module. You can achieve the same effect using the <VAR CLASS="literal" >Status</VAR > header. <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN33895" ></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">"Status: 404 Not Found"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV > </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 <VAR CLASS="literal" >REDIRECT</VAR > (302) status code to the browser unless some <VAR CLASS="literal" >3xx</VAR > status code has already been set. </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN33901" ></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 >Notã: </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 <VAR CLASS="literal" >$_SERVER['HTTP_HOST']</VAR >, <VAR CLASS="literal" >$_SERVER['PHP_SELF']</VAR > 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="AEN33910" ></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://" </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">dirname</font><font color="#007700">(</font><font color="#0000BB">$_SERVER</font><font color="#007700">[</font><font color="#DD0000">'PHP_SELF'</font><font color="#007700">])<br /> . </font><font color="#DD0000">"/" </font><font color="#007700">. </font><font color="#0000BB">$relative_url</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></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: </P ><P > <DIV CLASS="informalexample" ><P ></P ><A NAME="AEN33914" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// Date in the past<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">);<br /><br /></font><font color="#FF8000">// always modified<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">"Last-Modified: " </font><font color="#007700">. </font><font color="#0000BB">gmdate</font><font color="#007700">(</font><font color="#DD0000">"D, d M Y H:i:s"</font><font color="#007700">) . </font><font color="#DD0000">" GMT"</font><font color="#007700">);<br /> <br /></font><font color="#FF8000">// HTTP/1.1<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">"Cache-Control: no-store, no-cache, must-revalidate"</font><font color="#007700">);<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">"Cache-Control: post-check=0, pre-check=0"</font><font color="#007700">, </font><font color="#0000BB">false</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// HTTP/1.0<br /></font><font color="#0000BB">header</font><font color="#007700">(</font><font color="#DD0000">"Pragma: no-cache"</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 >Notã: </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 <VAR CLASS="literal" >session.cache_limiter</VAR > 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="AEN33927" ></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 >Notã: </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 <VAR CLASS="literal" >output_buffering</VAR > 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="AEN33938" ></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 >Notã: </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 >Notã: </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 <VAR CLASS="literal" >realm</VAR > part of the <VAR CLASS="literal" >WWW-Authenticate</VAR > 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" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.headers-list.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >headers_list</TD ></TR ></TABLE ></DIV ></BODY ></HTML >