内容长度和其他HTTP标头? [英] Content-length and other HTTP headers?
问题描述
如果我在生成普通HTML页面时设置此标题会给我带来任何好处吗?
Does it give me any advantage if I set this header when generating normal HTML pages?
我看到那里的一些框架会设置这个标题属性而我是想知道为什么...
(连同其他标题,如 Content-Type:text / html
)
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:他们这样做:
ob_start();
... stuff here...
$content = ob_get_contents();
$length = strlen($content);
header('Content-Length: '.$length);
echo $content;
推荐答案
我认为这仅仅是因为HTTP Spec对在每种情况下都这样做。
I think its only because of the HTTP Spec says to do this in every case possible.
应用程序应该使用此字段来指示消息正文的传输长度,除非这是禁止的按照4.4节中的规定。
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.
您还可以查看在Pauls答案的 Deaomon的问题。
我认为这也会回答你的问题。
I think this will answer yours too.
如果您希望有人下载带有另一个标题的文件,您也应该使用Content-Length:
例如
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);
?>
这篇关于内容长度和其他HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!