Perhaps the wording of my post is not clear enough. The headers must be sent before the response body. The response body is what appears when you "View source" in the browser, including all HTML and any characters (even spaces) before the HTML.

A complete response looks something like this:
Code:
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: foo=bar

<!DOCTYPE html>
<!-- ... -->