21

Does it give me any advantage if I set this header when generating normal HTML pages?

I see that some frameworks out there will set this header property and I was wondering why... (Along with other headers, like Content-Type: text/html)

Does browser load the site faster or smoother?

ps: they do it like:

ob_start();

... stuff here...

$content = ob_get_contents();
$length = strlen($content);

header('Content-Length: '.$length);

echo $content;
1
  • Answer to your question not listed below?
    – Neysor
    Mar 19, 2012 at 9:16

7 Answers 7

22

I think its only because of the HTTP Spec says to do this in every case possible.

Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.

You can also look at Pauls Answer on the Question of Deaomon.

I think this will answer yours too.

Also you should use Content-Length if you want someone download a file with another header: e.g.

<?php
$file = "original.pdf"
$size = filesize($file);
header('Content-type: application/pdf');
header("Content-length: $size");
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile($file);
?>
11

Does it give me any advantage if I set this header when generating normal html pages?

Does browser load the site faster or smoother?

For dynamically generated pages, usually not. Content-Length lets the client know how large the file is. However, if Content-Length isn't specified, the transfer is sent in chunks (with a special chunk that signifies the end of the file). The former method can result in faster transmission as the overhead of splitting into chunks is eliminated. However, this is usually best reserved for static files as the resources required to buffer the entire contents in order to determine the length may outweigh any advantages gained by setting Content-Length.

5
  • I checked the headers in developer tools from Chrome, and I see that the Content-Length header is set even if I don't set it in PHP.
    – ellabeauty
    Mar 15, 2012 at 22:03
  • 3
    If the content length is smaller than the OS/web server's send buffer size, the Content-Length will automatically be included by the web server. However, if the length exceeds the buffer, you will see that the content length will not be sent and, instead, Transfer-Encoding: chunked will be used.
    – webbiedave
    Mar 15, 2012 at 22:12
  • do you know how can I find out how much is the buffer size?
    – ellabeauty
    Mar 15, 2012 at 22:46
  • 2
    By default, Apache uses the OS buffer defaults for its SendBufferSize. To determine the OS buffer size, you'd need to search for tcp send buffer size <your_os> as it's different for every OS. For Windows XP-7, it's 8192 bytes.
    – webbiedave
    Mar 15, 2012 at 22:51
  • 2
    you're right, in chrome its 8K :) However I don't think it's reliable to use the content-length thing for html files, because they might be compressed by gzip, so the strlen() size wouldn't be the real size
    – ellabeauty
    Mar 15, 2012 at 23:00
2

The main motivation behind it is re-using an existing TCP connection in HTTP 1.1. Content-Length can demarcate where the response ends for the recipient. As for the other headers, like Content-Type which specifies the MIME-type, they are for the recipient so that she would know what to do with the result depending on the type of the content For example, in Firefox you can specify what action to perform with different MIME-types. If you can pop-up the browser's save dialog or open a PDF in viewer with Content-Type of application/pdf

4
  • you mean like requesting two pages in the same request?
    – ellabeauty
    Mar 15, 2012 at 21:20
  • No, they are sent in the context of the same TCP connection. The concept is called a HTTP persistent connection.
    – Uku Loskit
    Mar 15, 2012 at 21:23
  • hmmm but from what I'm reading in wikipedia in http 1.1 browser connections are persistent by default, so is this really needed?
    – ellabeauty
    Mar 15, 2012 at 21:26
  • That part is referring to Keep-Alive headers which are redundant in HTTP 1.1.
    – Uku Loskit
    Mar 15, 2012 at 21:30
2

Where content-type: text/html is concerned, it's mainly to tell the client what to expect and usually how to handle the file. In the case of bots, it can be useful for SEO purposes as it tells the bot what type of file it's looking at which can change how it's parsed/ranked.

In the case of content-length, it's just to let the client know how large a file to expect and thus where the response ends.

0

One supposedly good use for setting the content-length is when you like to send your response to the client even though you still like to perform more time-consuming operations afterwards.

See this answer that explains that you'd set the content length, write your output and then can perform further operations in your script without making the client wait any longer.

0

The best way (that is working for me) of getting size of a remote file:

$head = array_change_key_case(get_headers($file, TRUE));
$filesize = $head['content-length'];
0

Change

$content = ob_get_contents();

to

$content = ob_get_clean();
1
  • Why? Note that the question isn’t asking how to improve the code. It’s using it as an example of code they’ve seen in some frameworks. Their question is, “Does it give me any advantage if I set this header when generating normal HTML pages? ... Does browser load the site faster or smoother?” Your feedback on the code doesn’t answer that question. Jan 31, 2021 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.