php 内容长度和其他 HTTP 标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9728269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content-length and other HTTP headers?
提问by ellabeauty
Does it give me any advantage if I set this header when generating normal HTML pages?
如果我在生成普通 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
)
我看到一些框架会设置这个标题属性,我想知道为什么......(以及其他标题,比如Content-Type: text/html
)
Does browser load the site faster or smoother?
浏览器加载站点是否更快或更流畅?
ps: they do it like:
ps:他们这样做:
ob_start();
... stuff here...
$content = ob_get_contents();
$length = strlen($content);
header('Content-Length: '.$length);
echo $content;
回答by Neysor
I think its only because of the HTTP Spec says to do this in every case possible.
我认为这只是因为 HTTP 规范说在所有可能的情况下都这样做。
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.
应用程序应该使用这个字段来指示消息体的传输长度,除非 4.4 节中的规则禁止这样做。
You can also look at Pauls Answeron the Question of Deaomon.
您还可以查看Pauls对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.
如果您希望有人下载带有另一个标题的文件,您也应该使用 Content-Length:例如
<?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);
?>
回答by webbiedave
Does it give me any advantage if I set this header when generating normal html pages?
Does browser load the site faster or smoother?
如果我在生成普通 html 页面时设置这个标题,它会给我带来什么好处吗?
浏览器加载站点是否更快或更流畅?
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
.
对于动态生成的页面,通常不会。Content-Length
让客户知道文件有多大。但是,如果Content-Length
未指定,则传输以块的形式发送(带有表示文件结尾的特殊块)。前一种方法可以导致更快的传输,因为消除了分割成块的开销。但是,这通常最好保留给静态文件,因为缓冲整个内容以确定长度所需的资源可能超过通过设置Content-Length
.
回答by Uku Loskit
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
其背后的主要动机是重用 HTTP 1.1 中现有的 TCP 连接。Content-Length
可以为接收者划定响应的结束位置。至于其他标头,例如Content-Type
指定 MIME 类型的标头,它们用于接收者,以便她根据内容的类型知道如何处理结果例如,在 Firefox 中,您可以指定要执行的操作不同的 MIME 类型。如果你能弹出浏览器的保存对话框或浏览器中打开PDF用Content-Type
的application/pdf
回答by Shawn Solomon
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.
凡content-type: text/html
来说,这是主要是告诉客户端所期待的,通常如何处理该文件。在机器人的情况下,它可以用于 SEO 目的,因为它告诉机器人它正在查看的文件类型可以改变它的解析/排名方式。
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.
在 的情况下content-length
,它只是让客户端知道期望的文件有多大以及响应结束的位置。
回答by Mohsen Abasi
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'];
回答by Thomas Tempelmann
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 answerthat 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.