Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 3376

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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> &mdash; <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">&raquo;&nbsp;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">
&lt;html&gt;<br /><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;This&nbsp;will&nbsp;give&nbsp;an&nbsp;error.&nbsp;Note&nbsp;the&nbsp;output<br />&nbsp;*&nbsp;above,&nbsp;which&nbsp;is&nbsp;before&nbsp;the&nbsp;header()&nbsp;call&nbsp;*/<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Location:&nbsp;http://www.example.com/'</span><span style="color: #007700">);<br />exit;<br /></span><span style="color: #0000BB">?&gt;</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 &quot;<em>HTTP/</em>&quot; (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">&lt;?php<br />header</span><span style="color: #007700">(</span><span style="color: #DD0000">"HTTP/1.0&nbsp;404&nbsp;Not&nbsp;Found"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
        </div>

       </div>
      </p>
      <p class="para">
       The second special case is the &quot;Location:&quot; 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">&lt;?php<br />header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Location:&nbsp;http://www.example.com/"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">/*&nbsp;Redirect&nbsp;browser&nbsp;*/<br /><br />/*&nbsp;Make&nbsp;sure&nbsp;that&nbsp;code&nbsp;below&nbsp;does&nbsp;not&nbsp;get&nbsp;executed&nbsp;when&nbsp;we&nbsp;redirect.&nbsp;*/<br /></span><span style="color: #007700">exit;<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br />header</span><span style="color: #007700">(</span><span style="color: #DD0000">'WWW-Authenticate:&nbsp;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:&nbsp;NTLM'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</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">&raquo;&nbsp;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">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;We'll&nbsp;be&nbsp;outputting&nbsp;a&nbsp;PDF<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-type:&nbsp;application/pdf'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;It&nbsp;will&nbsp;be&nbsp;called&nbsp;downloaded.pdf<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Disposition:&nbsp;attachment;&nbsp;filename="downloaded.pdf"'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;PDF&nbsp;source&nbsp;is&nbsp;in&nbsp;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">?&gt;</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">&lt;?php<br />header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Cache-Control:&nbsp;no-cache,&nbsp;must-revalidate"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;HTTP/1.1<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Expires:&nbsp;Sat,&nbsp;26&nbsp;Jul&nbsp;1997&nbsp;05:00:00&nbsp;GMT"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Date&nbsp;in&nbsp;the&nbsp;past<br /></span><span style="color: #0000BB">?&gt;</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&#039;t cached even if you don&#039;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">&raquo;&nbsp;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">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Redirect&nbsp;to&nbsp;a&nbsp;different&nbsp;page&nbsp;in&nbsp;the&nbsp;current&nbsp;directory&nbsp;that&nbsp;was&nbsp;requested&nbsp;*/<br /></span><span style="color: #0000BB">$host&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</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">]),&nbsp;</span><span style="color: #DD0000">'/\\'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$extra&nbsp;</span><span style="color: #007700">=&nbsp;</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:&nbsp;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">?&gt;</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>