20

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;
James Douglas
  • 3,328
  • 2
  • 22
  • 43
ellabeauty
  • 2,529
  • 3
  • 21
  • 25

7 Answers7

21

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);
?>
Community
  • 1
  • 1
Neysor
  • 3,893
  • 11
  • 34
  • 66
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.

James Douglas
  • 3,328
  • 2
  • 22
  • 43
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • 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 '12 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 '12 at 22:12
  • do you know how can I find out how much is the buffer size? – ellabeauty Mar 15 '12 at 22:46
  • 2
    By default, Apache uses the OS buffer defaults for its [SendBufferSize](http://httpd.apache.org/docs/2.0/mod/mpm_common.html#sendbuffersize). To determine the OS buffer size, you'd need to search for `tcp send buffer size ` as it's different for every OS. For Windows XP-7, it's 8192 bytes. – webbiedave Mar 15 '12 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 '12 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

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
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

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'];
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mohsen Abasi
  • 2,050
  • 28
  • 30
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.

Community
  • 1
  • 1
Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
0

Change

$content = ob_get_contents();

to

$content = ob_get_clean();
Timax
  • 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. – Jeremy Caney Jan 31 '21 at 05:30