【问题标题】:Gzip data to be sent to browser in php在 php 中将 Gzip 数据发送到浏览器
【发布时间】:2011-09-10 22:35:34
【问题描述】:

我想 Gzip 数据(css,js)在 php 中发送到浏览器。我尝试了gzcompress、gzencode,但都使文件无法被浏览器识别

来自 firebug 的 home.css 数据

Date    Sat, 10 Sep 2011 22:31:59 GMT
Server  Apache/2.2.14 (Ubuntu)
X-Powered-By    PHP/5.3.2-1ubuntu4.9
Expires Sat, 17 Sep 2011 22:31:59 GMT
Cache-Control   public
Pragma  no-cache
Etag    e35b61f80bbf8e0dd722c50c65ec6da5
Vary    Accept-Encoding
Content-Encoding    gzip
Content-Length  25163
Keep-Alive  timeout=15, max=92
Connection  Keep-Alive
Content-Type    text/css

编辑:我什至在下面尝试过也没有用

<?php 
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
 ob_start("ob_gzhandler"); 
else 
ob_start(); 
?>

【问题讨论】:

  • 您使用的代码可能很有帮助,可能是标题有问题
  • ob_gzhandler 已经检查了 Accept-Encoding 标头以确定应如何对输出进行编码,您无需自己执行此操作。
  • 您发布的标头基本上看起来是正确的(它们包括正确的 Content-Encoding 和 Vary 标头)。您的输出可能已经在 Apache 级别进行了压缩,因此您也在 PHP 中进行双重编码。

标签: php apache http http-headers


【解决方案1】:

您需要设置appropriate headers 以便浏览器将文件识别为压缩文件。

然而,这确实是您的网络服务器处理的更好任务,而不是您的代码。

【讨论】:

  • @Web Developer“我什么都没得到”什么
  • 我的意思是链接的内容。链接的内容超出了我的想象
  • 网站没有加载?你不明白吗?你试过了还是不行?你想说什么?
  • 链接的内容超出了我的想象
【解决方案2】:

试试这个

<?php
ob_start("ob_gzhandler");
echo file_get_contents("stylesheet.css");
ob_end_flush();
?>

http://php.net/manual/en/function.ob-gzhandler.php

http://www.php.net/manual/en/function.ob-end-flush.php

刚刚用 apache 测试了这个,如果这不适合你,你在 apache 配置中搞砸了。

标题:

Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:180
Content-Type:text/html
Date:Sat, 10 Sep 2011 23:11:18 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
Vary:Accept-Encoding
X-Powered-By:PHP/5.3.5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    • 2014-05-05
    • 2014-03-11
    • 2021-10-12
    • 2013-05-12
    相关资源
    最近更新 更多