«
The HTTP Protocol
Contents
Important: If you missed the introduction to this PHP lab series and what follows confuses you then I recommend you take a step back and read it.
A Brief Introduction to HTTP
How does your browser fetch a resource on the Web and know what to do with it?
HTTP is the protocol that drives the WWW. It was conceived by Sir Tim Berners-Lee (that’s right, they knighted him). The Web is based on the client-server programming model in which the client (your browser) requests a resource (a Web page) from a server. A brief negotiation is made and the server returns the resource after which the browser renders the page and then you can view (or perhaps listen) to it. While all of that may seem complex and time consuming, HTTP is designed to be simple, and more importantly, very fast. This is one of the primary reasons the Web has been so successful.
If any of this is new to you, I cannot recommend highly enough this W3C document: Architecture of the World Wide Web, Volume One.
Each HTTP/1.1 transaction consists of a set of one line headers, starting with a request from the client:
Something to keep in mind is that these are not an arbitrary examples, the headers came directly from the request you made when you visited this page. Since PHP is installed as an extension module into my Web server software, which happens to be Apache (an open-source Web server, and by far the most popular one on the Internet), I have full access to the SAPI, meaning I can easily retrieve information about the request, and am able to return all sorts of interesting things in the response.
To break down the example, the first line is the resource you were interested in and is referred to, naturally, as the Request-Line:
- GET — the request method (POST is used for submitting forms)
- /vnav/labs/PHP/HTTP.php — the URL of the resource
- HTTP/1.1 — the protocol and version number
The other HTTP request headers include:
- Host: the domain and subdomain (www for example)
- User-Agent: a string that identifies your browser
- Accept: a list of media (
MIME
) types the client understands
FAQ: What are Media Types? In particular, review the IANA MIME Media Types and the W3C XHTML Media Types.
There are of course other headers, but we won’t get into that. The server responds:
Once again, these are not contrived examples but originated with the negotiation between my HTTP server and your browser when you requested this document. The Status-Line is another ordered set:
- HTTP/1.1 — the protocol and version number
- 200 — the status code, and in readable form (reason phrase):
- OK — in this case everything is fine, the document follows
Note: The status code might interest you if you’ve ever
requested a document that either doesn’t exist or was removed. This is the infamous 404
Not Found
page. HTTP/1.1 status codes are grouped into ranges — the
400 and 500 ranges are reserved for error conditions.
The other HTTP response headers include:
- Server: a string that identifies my server
- Content-type: the media type of the document I am sending
Notice that Content-type also includes the encoding (UTF-8) used for special characters. What interests us for this lab are the Accept (request) and Content-type (response) headers. Normally the server will automatically send the content type based on the extension of the file being requested — typically .html — which translates into the media type text/html. But if we want to serve our documents as XHTML we’ll have to override this default behavior. To do this we’ll be using the PHP header() function. You could of course serve static XHTML files as .xhtml, assuming your Web server is configured to do this, but we’re discussing dynamic PHP documents here.
Note: However, a common mistake is to make inferences based on the last segment in the path component of a URI.
For example, it is common to assume that a URI with a path that ends in
.html
identifies an HTML document.
In plain English: What a URI appears to be and what it actually is may be two very different things.
FYI: If this is new to you I should point out that the HTTP headers that make up the transaction between client and server are not normally visible to you. If this interests you, and you’d like to see more examples then I suggest you try a modern Mozilla-based browser such as Firefox (if you haven’t already). An excellent extension is available called LiveHTTPHeaders that will allow you to see every header exchanged between the client and the server for any request you make. You can install a similar plug-in for IE called ieHTTPHeaders.
The doctype() Function
In the next lab we will put all of this together into my PHP doctype() function.
It actually does quite a bit more that just send the DTD declaration. Depending
on the browser requesting the page, it will:
- Send the correct Content-type header in the HTTP response.
- Return the XML declaration.
- Print one of the seven DOCTYPEs depending on how you called it.
- Open the top level (root) element with the
<html>tag.

















































































