Page 1 of 1

Custom CMS, HTTP content-length header value

Posted: Mon Aug 22, 2011 6:38 am
by batfastad
Hi everyone

I've built a custom CMS (content management system) on our intranet to make it easy to manage the content across several websites that we manage. However I realised that nowhere in my website code am I outputting the HTTP content-length header. I send last-modified using the modification date of the article from the database but not content-length.
On our website the HTML code is output to the browser using a single echo() statement at the end of the script, as opposed to being sent to the browser in different stages.

When looking at the HTTP headers of my output I notice "transfer-encoding: chunked" instead of a content-length.
Is that a problem?
I believe that header is HTTP 1.1, not 1.0 so could that cause problems with some proxies/firewalls that people may use?

How do I calculate the content-length?
Do I need to use PHP's output buffering functions... ob_get_length() etc?
Or is there a way of manually calculating it without too much of a performance hit?
I'm assuming it's a bit more complicated than just running strlen() on my output variable?
And even though we use UTF-8 throughout, I guess strlen() would be better than mb_strlen() as the former gives me the value of bytes, the latter giving the number of characters in the string.

Cheers, B

Re: Custom CMS, HTTP content-length header value

Posted: Tue Aug 23, 2011 9:12 am
by greip
There is no need for you to supply the content length field.

The web server will add it if needed for HTTP/1.0 and use chunked transfer for HTTP/1.1 Chunked transfer is preferred as you can start sending data to the client as soon as you start generating data. No need to wait until everything has been generated in order to compute the total number of bytes in the document.