Custom CMS, HTTP content-length header value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Custom CMS, HTTP content-length header value

Post 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
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: Custom CMS, HTTP content-length header value

Post 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.
Post Reply