首页 > 编程技术 > php

php中ob_gzhandler' conflicts with 'zlib output compression问题

发布时间:2016-11-25 17:28

文章介绍了ob_gzhandler\' conflicts with \'zlib output compression解决方法

1. output handler ‘ob_gzhandler’ conflicts with ‘zlib output compression’

PHP Warning: ob_start() [ref.outcontrol]: output handler ‘ob_gzhandler’ conflicts with ‘zlib output compression’ in yourfiles


Try replacing

 

 代码如下 复制代码
if (extension_loaded('zlib')) {
$do_gzip_compress = TRUE;
ob_start();
ob_implicit_flush(0);
//header('Content-Encoding: gzip');
}
 

(i.e. the code that enables zlib compression) with

 

 代码如下 复制代码
if (extension_loaded('zlib')) {
ob_end_clean();
ob_start('ob_gzhandler');
}


 

这是因为PHP中Zlib模块设置错误造成的,将zlib.output_compression这一行注释掉就可以了。这里需要指出的是PHP5自带GZIP模块的,不需要另外加载。

mb_convert_encoding这个函数是用来转换编码的。原来一直对程序编码这一概念不理解,不过现在好像有点开窍了。 不过英文一般不会存在编码问题,只有中文数据才会有这个问题。

比如你用Zend Studio或Editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这时就要把数据进行编码转换,不然进到数据库就会变成乱码。

mb_convert_encoding的用法见官方:

mb_convert_encoding — Convert character encoding

Report a bug 说明
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
Converts the character encoding of string str to to_encoding from optionally from_encoding.

Report a bug 参数

str
The string being encoded.

to_encoding
The type of encoding that str is being converted to.

from_encoding
Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used.

See supported encodings.


Report a bug 返回值
The encoded string.

Report a bug 范例

Example #1 mb_convert_encoding() example

 代码如下 复制代码

<?php
/* Convert internal character encoding to SJIS */
$str = mb_convert_encoding($str, "SJIS");

/* Convert EUC-JP to UTF-7 */
$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");

/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");

/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
$str = mb_convert_encoding($str, "EUC-JP", "auto");
?>

mb_convert_encoding( $str, $encoding1,$encoding2 )

$str,要转换编码的字符串
$encoding1,目标编码,如utf-8,gbk,大小写均可
$encoding2,原编码,如utf-8,gbk,大小写均可

实例1

 代码如下 复制代码
<?php
$str='脚本之家:http://www.111cn.net';
echo mb_convert_encoding($str, "UTF-8"); //编码转换为utf-8
?>

 

 代码如下 复制代码
<?php
$str='脚本之家:http://www.111cn.net';
echo mb_convert_encoding($str, "UTF-8", "GBK"); //已知原编码为GBK,转换为utf-8
?>
 代码如下 复制代码
<?php
$str='脚本之家:http://www.111cn.net';
echo mb_convert_encoding($str, "UTF-8", "auto"); //未知原编码,通过auto自动检测后,转换编码为utf-8
?>

做一个GBK To UTF-8

 

 代码如下 复制代码
< ?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("???S我的友仔", "UTF-8", "GBK");
?>

再来个GB2312 To Big5

 

 代码如下 复制代码
< ?php
header("content-Type: text/html; charset=big5");
echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
?>

不过要使用上面的函数需要安装但是需要先enable mbstring 扩展库。

PHP中的另外一个函数iconv也是用来转换字符串编码的,与上函数功能相似。

下面还有一些详细的例子:

iconv — Convert string to requested character encoding
(PHP 4 >= 4.0.5, PHP 5)
mb_convert_encoding — Convert character encoding
(PHP 4 >= 4.0.6, PHP 5)

用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
需要先enable mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉
mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;


string iconv ( string in_charset, string out_charset, string str )
注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
Returns the converted string or FALSE on failure.


使用:

发现iconv在转换字符”—”到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个”—”都无法转换成功,无法输出。 另外mb_convert_encoding没有这个bug.

一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数.

from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used.

 代码如下 复制代码
/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);
/* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */
$str = mb_convert_encoding($str, “EUC-JP”, “auto”);

例子:

 代码如下 复制代码
$content = iconv(”GBK”, “UTF-8//IGNORE″, $content);
$content = mb_convert_encoding($content, “UTF-8″, “GBK”);

 

一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数

PHP的全局变量和C语言有一点点不同。在C语言中,全局变量在函数中主动生效,除非被局部变量笼罩。这可能引起一些问题,有些人可能漫不经心的转变一个全局变量。PHP中全局变量在函数中应用时必须用global申明为全局。

一、举例比较
例一:

 代码如下 复制代码
<?php  
 $var1 = 1;  
 function test(){  
     unset($GLOBALS['var1']);  
 }  
 test();  
 echo $var1;  
 ?>  


因为$var1被删除了,所以什么东西都没有打印。
例二:

 代码如下 复制代码
<?php  
 $var1 = 1;  
 function test(){  
     global  $var1;  
     unset($var1);  
 }  
 test();  
 echo $var1;  
 ?>

 
意外的打印了1。证明删除的只是别名引用,其本身的值没有受到任何的改变。

二、解释
    global $var其实就是&$GLOBALS['var'],调用外部变量的一个别名而已。
    上面代码中的$var1和$GLOBALS['var1']是指的同一变量,而不是两个不同的变量。
    PHP的全局变量和C语言有一点点不同。在C语言中,全局变量在函数中主动生效,除非被局部变量笼罩。这可能引起一些问题,有些人可能漫不经心的转变一个全局变量。PHP中全局变量在函数中应用时必须用global申明为全局。
    PHP的Global变量的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件。

三、结论
1.$GLOBALS['var']是外部的全局变量本身
2.global $var是外部$var的同名引用或者指针。

一个常用的东西,php+CKFinder上传中文名文件乱码问题的解决方法 这种问题主要是上传中文文件名的图片时出现乱码的解决办法。

上传中文名文件乱码问题

在ckfinder/config.php中找到如下一段配置代码:

 

 代码如下 复制代码
/*
If you have iconv enabled (visit http://php.net/iconv for more information),
you can use this directive to specify the encoding of file names in your
system. Acceptable values can be found at:
http://www.gnu.org/software/libiconv/

Examples:
$config['FilesystemEncoding'] = 'CP1250';
$config['FilesystemEncoding'] = 'ISO-8859-2';
*/
$config['FilesystemEncoding'] = 'UTF-8';

将UTF-8修改为GB2312,上传后文件名正确了,但在CKEditor中显示的链接出现乱码,因为CKEditor所在页面使用的字符集是UTF-8,未去细究如何解决这个问题,采用了文件重命名的方案去替代解决。

上传文件重命名

修改ckfindercoreconnectorphpphp5CommandHandlerFileUpload.php

找到以下代码

 代码如下 复制代码
if ($sFileName != $sUnsafeFileName) {
  $iErrorNumber = CKFINDER_CONNECTOR_ERROR_UPLOADED_INVALID_NAME_RENAMED;
}
在这段代码之后添加
 代码如下 复制代码
$sExtension=CKFinder_Connector_Utils_FileSystem::getExtension($sFileName);
$sFileName=date('YmdHis').'.'.$sExtension;
谈到了使用require_once 仍然告诉说类被重定义的问题。于是想起了前几天自己遇到的现象。在这里和大家说一下,今天才想起调查具体原因,如果大家不说的话就快忘了。看来自己凡事果然不经大脑,大脑的二级缓存,及内存,甚至是硬盘都小的可怜。嗯。

假设有如下三个文件, c.php a.php b.php 对应的存放目录为:localhost/ localhost/ localhost/demo

 代码如下 复制代码
c.php
require_once("a.php");
require_once("demo/b.php");
B::demo();a.php
class A
{
}

b.php的内容比较有意思,因为它自己要继承 CLASS A 所以自己把a.php也引入进去了

 代码如下 复制代码
require_once("../a.php");
class B extends A
{
    public static function demo()
    {
    echo "xx";
    }
}

执行localhost/c.php 系统报错,报错信息如下
Warning: require_once(../a.php) [function.require-once]: failed to open stream: No such file or directory in F:wwwdemob.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '../a.php' (include_path='.;C:php5pear') in F:wwwdemob.php on line 2但是,惊奇的发现,如果去掉b.php里面的require_once语句,执行正常,那么一定是require_once语句定义多了吗?原因就是Class A重定义了两次?可是不会啊。如果我只在c.php里面加require_once(‘a.php’);这条语句,哪怕我写两遍也是没错的,那到底是咋回事呢?
原因就是,b.php定义的目录和c.php执行文件的目录层级不一致,导致在c.php里面require_once语句有两条。使其相当于

 代码如下 复制代码
require_once("a.php");
require_once("../a.php");
class B extends A
{
    public static function demo()
    {
    echo "xx";
    }
}
B::demo();

原因找到了,因为在c.php里面,其相对目录 “..”就是 c.php的上一层了,导致文件找不到报错。
所以,我们的结论是,在 PHP 里面,使用require_once的时候,存在不同层级关系,且有相对目录的使用那么一定要谨慎,小心。

标签:[!--infotagslink--]

您可能感兴趣的文章: