小编M.A*_*hmi的帖子

PHP手动GZip编码

我用Page Speed测试我的网站,结果大约是70/100.启用压缩是减慢速度的第一个也是最重要的因素.

我知道我可以通过修改php.ini来自动执行此操作,但我对手动方法(gzencode)更感兴趣.

问题是所有浏览器都无法打开网站(Firefox:"您尝试查看的页面无法显示,因为它使用的是无效或不受支持的压缩形式.",Chrome:"303,ERR内容编码"等. )或者它们显示编码的字符串.

Live Headers显示浏览器接受编码,并且响应已设置内容类型,但仍然失败.

GET / HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate

HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Length: 5827
Vary: Accept-Encoding
Run Code Online (Sandbox Code Playgroud)
private function _compress($data) {
    //return trim(preg_replace(array('/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s'), array('>','<','\\1'), $data));
    $supportsGzip = strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false;

    ob_start();
    if ($supportsGzip) {
        echo gzencode(trim(preg_replace('/\s+/', ' ', $data)), 9);
    } else {
        echo $data;
    }

    $content = ob_get_contents();
    header("content-type: text/html; charset: UTF-8");
    header("cache-control: must-revalidate");
    $offset = 60 * 60;
    $expire = "expires: " . gmdate("D, d M Y …
Run Code Online (Sandbox Code Playgroud)

php compression encoding gzip

13
推荐指数
2
解决办法
3万
查看次数

标签 统计

compression ×1

encoding ×1

gzip ×1

php ×1