mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
cleaned up header(), incorporated all notes.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@61274 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
b73e38061b
commit
f1883aab78
1 changed files with 110 additions and 45 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.29 $ -->
|
||||
<!-- $Revision: 1.30 $ -->
|
||||
<reference id="ref.http">
|
||||
<title>HTTP functions</title>
|
||||
<titleabbrev>HTTP</titleabbrev>
|
||||
|
@ -26,34 +26,32 @@
|
|||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
The <function>header</function> function is used at the top of an
|
||||
<acronym>HTML</acronym> file to send raw <acronym>HTTP</acronym>
|
||||
header strings. See the <ulink url="&spec.http1.1;">HTTP 1.1
|
||||
Specification</ulink> for more information on raw http headers.
|
||||
<function>header</function> is used to send raw
|
||||
<acronym>HTTP</acronym> headers. See the <ulink
|
||||
url="&spec.http1.1;">HTTP/1.1 specification</ulink> for more
|
||||
information on <acronym>HTTP</acronym> headers.
|
||||
</para>
|
||||
<para>
|
||||
The optional <parameter>replace</parameter> 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 &false; as
|
||||
the second argument you can force multiple headers of the same type. For
|
||||
example:
|
||||
The optional <parameter>replace</parameter> 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 &false; as the second argument you can force
|
||||
multiple headers of the same type. For example:
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
header('www-authenticate: Negociate');
|
||||
header('www-authenticate: NTLM',false);
|
||||
header('WWW-Authenticate: Negotiate');
|
||||
header('WWW-Authenticate: NTLM',false);
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
<para>
|
||||
There are two special-case header calls. The first is the
|
||||
"Location" header. Not only does it send this header
|
||||
back to the browser, it also returns a REDIRECT status code to
|
||||
Apache. From a script writer's point of view this should not be
|
||||
important, but for people who understand Apache internals it is
|
||||
important to understand.
|
||||
"Location" header. Not only does it send this header back
|
||||
to the browser, but it also returns a <literal>REDIRECT</literal>
|
||||
(302) status code to the browser.
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
header ("Location: http://www.php.net/"); /* Redirect browser
|
||||
header("Location: http://www.php.net/"); /* Redirect browser
|
||||
to PHP web site */
|
||||
exit; /* Make sure that code below does
|
||||
not get executed when we redirect. */
|
||||
|
@ -62,71 +60,138 @@ exit; /* Make sure that code below does
|
|||
</para>
|
||||
<note>
|
||||
<para>
|
||||
HTTP 1.1 requires an absolute URI as argument to
|
||||
HTTP/1.1 requires an absolute <acronym>URI</acronym> as argument to
|
||||
<ulink url="&spec.http1.1;-sec14.html#sec14.30">Location:</ulink>
|
||||
including protocol, hostname and absolute path. Some clients
|
||||
might accept relative URIs but you definetly should not rely on
|
||||
it. You can usually use $HTTP_SERVER_VARS['HTTP_HOST'],
|
||||
$HTTP_SERVER_VARS['PHP_SELF'] and <function>dirname</function>
|
||||
to make an absolute URI from a relative one yourself:
|
||||
including the scheme, hostname and absolute path, but
|
||||
some clients accept relative URIs. You can usually use
|
||||
$HTTP_SERVER_VARS['HTTP_HOST'], $HTTP_SERVER_VARS['PHP_SELF']
|
||||
and <function>dirname</function> to make an absolute URI from a
|
||||
relative one yourself:
|
||||
<informalexample>
|
||||
<programlisting>
|
||||
header ("Location: http://".$HTTP_SERVER_VARS['HTTP_HOST']
|
||||
."/".dirname($HTTP_SERVER_VARS['PHP_SELF'])
|
||||
."/".$relative_url);
|
||||
header("Location: http://".$HTTP_SERVER_VARS['HTTP_HOST']
|
||||
."/".dirname($HTTP_SERVER_VARS['PHP_SELF'])
|
||||
."/".$relative_url);
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
</note>
|
||||
<para>
|
||||
The second special-case is any header that starts with the
|
||||
string, "HTTP/" (case is not significant). For
|
||||
example, if you have your ErrorDocument 404 Apache directive
|
||||
pointed to a PHP script, it would be a good idea to make sure
|
||||
that your PHP script is actually generating a 404. The first
|
||||
thing you do in your script should then be:
|
||||
The second special case is any header that starts with the
|
||||
string, "<literal>HTTP/</literal>" (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 <literal>ErrorDocument</literal> directive), you may want to
|
||||
make sure that your script generates the proper status code.
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
header ("HTTP/1.0 404 Not Found");
|
||||
header("HTTP/1.0 404 Not Found");
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
<note>
|
||||
<para>
|
||||
In PHP 3, this only works when PHP is compiled as an Apache
|
||||
module. You can achieve the same effect using the
|
||||
<literal>Status</literal> header.
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
header("Status: 404 Not Found");
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
</para>
|
||||
</note>
|
||||
</para>
|
||||
<para>
|
||||
PHP scripts often generate dynamic HTML that must not be cached
|
||||
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
|
||||
<informalexample>
|
||||
<programlisting role="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-cache, must-revalidate"); // HTTP/1.1
|
||||
header ("Pragma: no-cache"); // HTTP/1.0
|
||||
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
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
<note>
|
||||
<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.
|
||||
</para>
|
||||
<para>
|
||||
Additionally, <function>session_cache_limiter</function> and
|
||||
the <literal>session.cache_limiter</literal> configuration
|
||||
setting can be used to automatically generate the correct
|
||||
caching-related headers when sessions are being used.
|
||||
</para>
|
||||
</note>
|
||||
</para>
|
||||
<para>
|
||||
Remember that the <function>header</function> function must be
|
||||
Remember that <function>header</function> 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 <function>include</function>, or
|
||||
<function>require</function>, functions, or another file access
|
||||
function, and have spaces or empty lines that will output before
|
||||
function, and have spaces or empty lines that are output before
|
||||
<function>header</function> is called. The same problem exists
|
||||
when using a single PHP/HTML file.
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<?php require("user_logging.inc") ?>
|
||||
<?php require("user_logging.inc") ?>
|
||||
|
||||
|
||||
<?php header ("Content-Type: audio/x-pn-realaudio"); ?>
|
||||
<?php header ("Content-type: audio/x-pn-realaudio"); ?>
|
||||
// Broken, Note the blank lines above
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
<note>
|
||||
<para>
|
||||
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
|
||||
<function>ob_start</function> and <function>ob_end_flush</function>
|
||||
in your script, or setting the <literal>output_buffering</literal>
|
||||
configuration directive on in your <filename>php.ini</filename> or
|
||||
server configuration files.
|
||||
</para>
|
||||
</note>
|
||||
</para>
|
||||
<para>
|
||||
See also <function>headers_sent</function>
|
||||
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 <ulink
|
||||
url="&url.rfc1806;">Content-Disposition</ulink> header to
|
||||
supply a recommended filename and force the browser to display the
|
||||
save dialog.
|
||||
<informalexample>
|
||||
<programlisting role="php">
|
||||
<?php
|
||||
header("Content-type: application/pdf");
|
||||
header("Content-Disposition: attachment; filename=downloaded.pdf");
|
||||
|
||||
/* ... output pdf file ... */
|
||||
</programlisting>
|
||||
</informalexample>
|
||||
<note>
|
||||
<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.
|
||||
</para>
|
||||
</note>
|
||||
</para>
|
||||
<para>
|
||||
See also <function>headers_sent</function>,
|
||||
<function>setcookie</function>, and the section on
|
||||
<link linkend="features.http-auth">HTTP authentication</link>.
|
||||
</para>
|
||||
</refsect1>
|
||||
</refentry>
|
||||
|
|
Loading…
Reference in a new issue