2011-09-10 48 views
0

我想Gzip已数据(CSS,JS)被发送到浏览器在PHP。我试过gzcompressgzencode但都通过浏览器从萤火虫发文件面目全非Gzip已数据被发送到浏览器在PHP

数据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(); 
?> 
+0

你可以使用证明是有益的,可能出错ob_gzhandler'已经检查了Accept-Encoding头,以确定输出应该如何编码,你不这样做的头 –

+0

'代码需要自己做。 –

+0

您发布的标题基本上看起来正确(它们包括正确的Content-Encoding和Vary标题)。您的输出可能已经在Apache级别进行了压缩,因此您也可以通过PHP进行双重编码。 –

回答

0

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

这确实是不过为您的Web服务器来处理,而不是你的代码更好的任务。

+0

我没有得到任何 – aWebDeveloper

+0

@Web开发者“我没有得到任何东西”我的意思是 – JohnD

+0

链接的内容。链接的内容覆盖了我的脑袋 – aWebDeveloper

0

试试这个

<?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 
相关问题