name of the Web server, to which the browser makes a connection. The browser must tell
the Web server which document it wants, and it does so using the HTTP protocol. Before
completing the request, the browser may provide lines of extra information called
headers. These headers let the server know the brand of the browser, the type of
documents the browser can accept, perhaps even the URL of a referring page.
The Web server places these headers into environment variables to conform with the
Common Gateway Interface (CGI). When a PHP script begins, the environment variables
are converted into PHP variables. One of the most useful headers describes the brand and
version of the Web browser. This header is sent by the browser as User-agent. The Web
server creates an environment variable called HTTP_USER_AGENT that holds the value of
the header. PHP in turn creates a variable with this same name. You can refer to it using
$, just as for any other variable. If you are using Apache, you also have the option of
using the getallheaders function. It returns an array of all headers exchanged between
the browser and the server.
As a PHP script begins to execute, the HTTP exchange is in the stage where some
headers have been sent to the browser, but no content has. This is a window of
opportunity to send additional headers. You can send headers that cause the browser to
ask for authentication, headers that request that the browser cache a page, or headers that
redirect the browser to another URL. These are just some of the many HTTP headers you
can send using the header function. The most common tasks are described in the last
section of this book.
Headers are placed on a stack, which is a data structure that resembles a literal stack of
dinner plates. Imagine that each plate is a header. Each new plate is placed atop the
previous plate. When it's time to send the headers, they are removed from the top, one at
a time. This has the effect of sending the headers to the browser in the reverse of the
order in which they were added. Usually this has no effect. HTTP doesn't define any
special meaning to the order of headers. However, if you send the same header twice, the
later header may overwrite the value of the earlier. This means that if you try resending a
header, the browser most likely will ignore it. My advice is to write your scripts so they
send headers only when they are certain of the value.
Once any content is sent, the opportunity to send headers is lost. This includes any text
outside of PHP tags, even if it's just a linefeed. If you try to send a header after content is
sent, an error message is generated. You can use the headers_sent function to test
whether it's safe to add more headers to the stack, or too late. Cookies, described below,
use headers, and therefore are limited in the same way.
As a script runs and sends content, the output is buffered. There is a bit of overhead to
every network action, so a small amount of memory temporarily stores the information to
be sent out in batches. This buffer is owned by the Web server, so PHP does not have
control of it. However, you may request that the buffer be flushed—immediately sent to
the browser—by using the flush function. This is most useful in long scripts. Both
browsers and people have limits to how long they wait for a response, so you can let them