开发者

Gzip data to be sent to browser in php

I want to Gzip data(css,js) being sent to browser in php . I tried gzcompress, gzencode but both made the file unrecognizable by the browser

Data from firebug for 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

EDIT: I even tried below that too didn't work

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


You need to set the appropriate headers in order for the browser to recognize the file as being compressed.

This really however is a better task for your web server to handle, rather than your code.


try this

<?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

just tested this with apache, if this isnt working for you you messed something up in your apache config.

headers:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜