PHP being a server side scripting language can interact with web servers in many ways. One of those is through the use of HTTP headers.
HTTP headers are ways of sending information that describes the content of the HTML (or other type of file). For instance, a HTTP header request for http://www.jamiebalfour.scot/ looks like:
Connect to 220.127.116.11 on port 80 ... ok GET / HTTP/1.1[CRLF] Host: jamiebalfour.scot[CRLF] Connection: close[CRLF] User-Agent: Web-sniffer/1.1.0 (+http://web-sniffer.net/)[CRLF] Accept-Encoding: gzip[CRLF] Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF] Cache-Control: no-cache[CRLF] Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF] Referer: http://web-sniffer.net/[CRLF]
Headers in PHP
PHP allows the manipulation of headers using the
<?php header($headerString); ?>
$headerString is used to define the header information that
would be sent. For instance, a PHP redirect to http://www.jamiebalfour.scot/
could be specified with:
<?php header("Location: http://www.jamiebalfour.scot/"); exit(); ?>
exitfunction to immediately stop any further output! This header is required to be the only thing sent to the client, so any further HTML and it will not function.
There are several other PHP supported headers which are described in the table below:
|Cache-Control: no-cache, must-revalidate||Prevents caching of a page|
|Content-Type: application/pdf||Modify the content type and get PHP to emulate it's file type.|
|Content-Disposition: attachment; filename="file.pdf"||Modify the header file name when a file is being downloaded. This way the file can be given it's original name.|
Things to observe with headers
HTTP headers absolutely must be defined before any output is given out. That means that the header must be before any HTML or text. This even includes whitespace that is not within the PHP block.
Without following this rule, PHP may throw errors or warning on to the page as well.
Files with headers should absolutely not store with a byte-order-mark (BOM). The byte-order-mark that will appear normally
appears as the hexadecimal
EF BB BF. There is more on this here.