内容长度和其他 HTTP 标头?

2024-08-22php开发问题
6

本文介绍了内容长度和其他 HTTP 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如果我在生成普通 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 规范要求在所有可能的情况下都这样做.

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 Answer 上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 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17