How To Respond To HTTP HEAD Request From PHP

By Angsuman Chakraborty, Gaea News Network
Thursday, March 1, 2007

Most (well written) PHP programs recognize GET from POST requests and handle them appropriate. However most of them are not aware of HEAD requests and handle them identical to GET. This is a waste of resources and is in contravention to HTTP 1.1 protocol (RFC 2616).

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

In short, unless you are setting special headers in response to a GET request you can simply exit when a HEAD request is received. The sample code will be:


if (stripos($_SERVER['REQUEST_METHOD'], 'HEAD') !== FALSE) {
    exit();
}

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :