如何设置正确的内容编码
我在 HTTP 标头:内容编码方面遇到了一些严重的(或者更好地说:奇怪)问题。
我想在将内容发送到客户端浏览器之前对其进行 gzip 压缩。为此,我检查客户端浏览器是否接受 gzip,如果是,我使用 ob_start("ob_gzhandler")
并设置内容编码: $response->addHeader("Content- Encoding", "gzip");
我认为我的问题是 Content-Encoding 标头的手动设置。 如果我使用
$response->addHeader("Content-Encoding", "gzip");
内容仅在 Opera 中显示。如果我使用
$response->addHeader("Content-Encoding", "'gzip'");
内容在所有浏览器中显示正确,但 gzip 压缩检查表明它是未压缩,W3C HTML 验证服务无法对页面进行编码:
错误是:不知道如何解码内容编码“gzip”
- 如果我不使用该行并在线使用
ob_start("ob_gzhandler")
该页面只能显示在opera
我在浏览器中正确输出的完整代码行如下:
$accEncoding = $request->getHeader("http_accept_encoding");
if($accEncoding !== NULL && substr_count($accEncoding, 'gzip')) {
ob_start("ob_gzhandler");
$response->addHeader("Content-Encoding", "'gzip'");
$response->addHeader("Vary", "Accept-Encoding");
} else {
ob_start();
}
我是否使用了 ob_gzhandler 错误或者我在这里犯了任何其他错误?我对 gzip 输出的正确处理感到非常困惑。
I have some serious (or better say: strange) issues with the HTTP-header: Content-Encoding.
I want to gzip my content before sending it to the clients browser. For this I am checking if the clients browser accepts gzip and if so I am using ob_start("ob_gzhandler")
and setting the content-encoding: $response->addHeader("Content-Encoding", "gzip");
I think my problem is the manual setting of the Content-Encoding header.
If I use$response->addHeader("Content-Encoding", "gzip");
the content is only shown in Opera.If I use
$response->addHeader("Content-Encoding", "'gzip'");
the content is shown correct in all browsers, but gzip compression checks say that it is not compressed and the W3C HTML Validation service cannot encode the page:
The error was: Don't know how to decode Content-Encoding ''gzip''
- If I don't use the line and online use
ob_start("ob_gzhandler")
the page can only be shown in opera
My complete lines of code, which does correct output in browser is following:
$accEncoding = $request->getHeader("http_accept_encoding");
if($accEncoding !== NULL && substr_count($accEncoding, 'gzip')) {
ob_start("ob_gzhandler");
$response->addHeader("Content-Encoding", "'gzip'");
$response->addHeader("Vary", "Accept-Encoding");
} else {
ob_start();
}
Am I using the ob_gzhandler wrong or am I doing any other mistake here? I am very confused about the correct handling of gzip output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ob_gzhandler
已验证浏览器是否支持 gzip 压缩:它还相应地设置内容编码标头。
ob_gzhandler
already verifies that the browser supports gzip compression:It also sets the Content-Encoding header accordingly.