PHP手册GZip编码 [英] PHP Manual GZip Encoding

查看:58
本文介绍了PHP手册GZip编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



实时标题显示浏览器接受该编码,并且响应已设置了内容类型,但仍然失败。

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

HTTP / 1.1 200 OK
内容编码:gzip
内容长度:5827
变化:接受编码



< hr>

 私有函数_compress($ data){
//返回修剪(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();
标头(内容类型:文本/ html;字符集:UTF-8);
标头(缓存控制:必须重新验证);
$ offset = 60 * 60;
$ expire =过期:。 gmdate( D,d M Y H:i:s,time()+ $ offset)。 格林威治标准时间;
标头($ expire);
标头(’Content-Length:’。strlen($ content));
标头( Vary:Accept-Encoding);
ob_end_clean();
echo $ content;
}

如果将Content-Encoding更改为zlib,则会得到编码后的字符串:

  ‹      ÕZÿsÛ¶ÿW^'¥²o'¨/-Ë–Ú؉_Ôµ•õÚ_v I°I,!A©j–Öºnçÿb·»%ÍÚë²nëå?'þ› =€¤L)',ÛIw>ŸEâxïáƒ÷°ùÞ½½O¶Ÿï߇Žtlصµ»$kŸ•¶ã^ã<܃•\¾ …Ÿº—\¸Ô6ŒûŽ ^Õ0z½^®WÊ¿m4ÅjÅ°…Xé©Ã¦ænS·]#ÌÕF-|8LRPL²ìIÈ»5²-\É\™mô=FÀŒJ5Ù-RóÝ ³Cý€ÉZ ([[ÙŠb%¹´YýÑãáîcx}±iD´〜¿KV#4á§x>¬°à®íÒãpÅËæî1øÌ®'@öm

我真的不太在乎获得压缩,因为我想知道为什么压缩不起作用。



干杯,

解决方案

好吧,我认为这是因为您正在尝试压缩空字符串。



我按照您的要求拿了您的脚本,并在FF和IE中运行它。



两者都失败了,FF表示存在问题(



但是,然后我注意到$ data是一个空字符串。



当我设置 $ data =一些测试数据。; 文件顶部立即生效(浏览器显示一些测试数据。),并在Firebug中检入,可以看到正确的标题。

 内容编码gzip 
内容长度68
可变接受编码
内容类型text / html

编辑:另外,要指出的是,您的 if($ supportsGzip){是有点奇怪,因为您的else条件实际上应该回显 $ data ,而不是 $ content



编辑:好的,根据您上面修改的功能,有两个关键问题。



主要问题与以下事实有关:您通过调用 ob_end_clean()来清除标题。对 PHP文档的评论指出, ob_end_clean()确实可以



这意味着您在调用 ob_end_clean()之前设置的任何标题都将被擦除。另外,您修改后的函数也不会发送gzip编码标头。



我必须说,在这里甚至都不需要使用ob_start和相关函数。尝试以下操作:

  function _compress($ data){

$ supportsGzip = strpos($ _SERVER ['HTTP_ACCEPT_ENCODING'],'gzip')!== false;


if($ supportsGzip){
$ content = gzencode(trim(preg_replace(‘/ \s + /’,’’,$ data)),9);
标头(内容编码:gzip);
} else {
$ content = $ data;
}

$ offset = 60 * 60;
$ expire =过期:。 gmdate( D,d M Y H:i:s,time()+ $ offset)。 格林威治标准时间;

标头( content-type:text / html; charset:UTF-8);
标头(缓存控制:必须重新验证);
标头($ expire);
标头(‘Content-Length:’。strlen($ content));
标头( Vary:Accept-Encoding);

echo $ content;

}

_compress(某些测试数据);

这在IE和FF中有效,但我没有时间测试其他浏览器。



如果确实必须使用ob_start和相关函数,请确保在调用 ob_end_clean()之后设置标题。 / p>

I was testing my website with Page Speed and the result was around 70/100. Enable Compression was the first and most important factor in slowing it down.

I know that I can do that by modifying the php.ini to automatically do that but I was more interested in the manual method (gzencode).

The problem is either all browsers fail in opening the website (Firefox: "The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.", Chrome: "303, ERR Content Encoding", etc.) or they display the encoded string.

Live Headers shows that the browser accepts the encoding, and the response has the content type set, but it still fails.

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


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 H:i:s", time() + $offset) . " GMT";
    header($expire);
    header('Content-Length: ' . strlen($content));
    header('Vary: Accept-Encoding');
    ob_end_clean();
    echo $content;
}

If I change the Content-Encoding to zlib, I get the encoded string:

‹������ÕZÿsÛ¶ÿW^‘¥²o‘¨/–-Ë–Ú؉_Ôµ•õÚ_v I°I‚!A©j–Öºnçÿb·»%ÍÚë²nëå?‘þ›=€¤L)’,ÛIw>ŸEâxïáƒ÷°ùÞ½O¶Ÿï߇Žtlؼµ·» $kŸ•¶ ã^ã<܃•\¾� Ÿº—\¸Ô6ŒûŽ"^Õ0z½^®WÊ ¿m4ÅjÅ°…XÎ’©Ã¦ænS·]#ÌÕF-|8LRPL²ìIÈ»5²-\É\™mô=FÀŒJ5"Ù—RóÝ�³Cý€ÉZ([ÙŠb%¹´YýÑãáîcx}±iD´˜¿KV#4"á§x>¬°à®íÒ ãpÅËæî1øÌ®‘@öm

I don’t really care anymore about getting the compression as much as I want to know why its not working.

Cheers,

解决方案

Well, I think it's because you're trying to compress an empty string.

I took your script as you gave it, and ran it in FF and IE.

Both failed, and FF said that there was an issue (like you described).

However, I then noticed $data is an empty string.

When I set $data = "Some test data."; at the top of the file it worked immediately (browser displayed "Some test data."), and checking in Firebug, I can see the correct headers.

Content-Encoding    gzip
Content-Length  68
Vary    Accept-Encoding
Content-Type    text/html

Edit: Also, just to point out, your if ($supportsGzip) { is a bit odd, because your else condition should actually echo out $data, not $content.

Edit: Okay, based on your revised function above, there are two key problems.

The primary problem has to do with the fact that you're wiping out your headers by calling ob_end_clean(). A comment on the PHP Docs states that "ob_end_clean() does discard headers".

This means any headers you set before calling ob_end_clean() will get wiped. Also, your revised function doesn't send a gzip encoding header, either.

I must say that there is probably no need to even use ob_start and related functions here, either. Try the following:

function _compress( $data ) {

    $supportsGzip = strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;


    if ( $supportsGzip ) {
        $content = gzencode( trim( preg_replace( '/\s+/', ' ', $data ) ), 9);
        header('Content-Encoding: gzip');
    } else {
        $content = $data;
    }

    $offset = 60 * 60;
    $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";

    header("content-type: text/html; charset: UTF-8");
    header("cache-control: must-revalidate");
    header( $expire );
    header( 'Content-Length: ' . strlen( $content ) );
    header('Vary: Accept-Encoding');

    echo $content;

}

_compress( "Some test data" );

This works in IE and FF, but I didn't have time to test other browsers.

If you really must use ob_start and related functions, make sure you set your headers after you call ob_end_clean().

这篇关于PHP手册GZip编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆