2011-10-10 52 views
3

我正在努力寻找解决方案来继续使用Suhosin补丁,并使其能够使用UTF-8表单提交。这是非常简单的测试我做:使用字符串iñtërnâtiônàlizætiønPHP 5.3,Suhosin和UTF-8

<?php var_dump($_POST); ?> 
<form method="post"> 
    <input name="test" type="text"/> 
    <input type="submit" /> 
</form> 

。 显然,我首先在服务器上启用utf-8头文件,并将Php default_charset设置为utf-8,并且启用了mb *重写。 只要我禁用Suhosin补丁并重新提交表单,一切都按原样运行。

UPDATE

我做更多的测试,只是可以肯定的:

$test = $_POST['test']; 

var_dump(mb_detect_encoding($test, "UTF-8", true)); 

// Returns true if $string is valid UTF-8 and false otherwise. 
function is_utf8($string) { 

    // From http://w3.org/International/questions/qa-forms-utf-8.html 
    return preg_match('%^(?: 
     [\x09\x0A\x0D\x20-\x7E]   # ASCII 
    | [\xC2-\xDF][\x80-\xBF]    # non-overlong 2-byte 
    | \xE0[\xA0-\xBF][\x80-\xBF]  # excluding overlongs 
    | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 
    | \xED[\x80-\x9F][\x80-\xBF]  # excluding surrogates 
    | \xF0[\x90-\xBF][\x80-\xBF]{2}  # planes 1-3 
    | [\xF1-\xF3][\x80-\xBF]{3}   # planes 4-15 
    | \xF4[\x80-\x8F][\x80-\xBF]{2}  # plane 16 
    )*$%xs', $string); 

} // function is_utf8 
var_dump(is_utf8($test)); 

两者的测试返回false启用和真否则了Suhosin补丁。问题是:它是一个错误还是预期的行为?是否有Suhosin补丁的配置参数对多字节字符串做了一些神奇的事情?

我在这里看到的唯一选择是禁用该补丁,除非一位聪明的头脑给出正确的建议。

更新2

的GET字符串不破坏,并正确显示在浏览器中。目前只有POST进行。

+0

你应该得到某种形式的错误。同时检查你的错误日志。我也强烈建议不要使用mb_函数覆盖。他们将搞砸了很多现有的代码。最好在你需要时总是明确地使用mb_函数。 – Evert

+0

我看不到任何错误:字符串只是错误地解码 – zekus

+1

它是如何解码不正确?你甚至没有提到你的问题。添加实际问题的详细信息,而不是'它无效' – Evert

回答

0

从谷歌搜索,我发现http://algorytmy.pl/doc/php/ref.mbstring.php其中提到

Beginning with PHP 4.3.3, if enctype for HTML form is set to multipart/form-data and mbstring.encoding_translation is set to On in php.ini the POST'ed variables and the names of uploaded files will be converted to the internal character encoding as well. However, the conversion isn't applied to the query keys.

这对我来说并不重要,但它确实提到了似乎是POST的POST变量关键问题。

我发现,如果我把这个在我的Apache的虚拟主机,我可以重现你的问题:

php_admin_value mbstring.language  "Neutral" 
php_admin_value mbstring.encoding_translation "On" 
php_admin_value mbstring.http_input  "UTF-8" 
php_admin_value mbstring.http_output "UTF-8" 
php_admin_value mbstring.detect_order "auto" 
php_admin_value mbstring.substitute_character "none" 
php_admin_value mbstring.internal_encoding "UTF-8" 
php_admin_value mbstring.func_overload "7" 
php_admin_value default_charset "UTF-8" 

以供参考,这是PHP测试页我用重现该问题:

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
<pre><?php echo $_POST['test'];?></pre> 
<form method="post"> 
    <input name="test" type="text"/> 
    <input type="submit" /> 
</form> 
Test string to use: iñtërnâtiônàlizætiøn 
</body> 
</html> 

我试图注释出以下MBSTRING设置(或将其关闭):

; Disable HTTP Input conversion (PHP 4.3.0 or higher) 
mbstring.encoding_translation = Off 

Ť他似乎解决了这个问题,尽管它对我来说没有什么意义,因为编码为的内部字符是 utf-8?

我注意到另一个奇怪的是,如果我直接在php.ini(而不是Apache的虚拟主机)设置这些mbstring值,我无法重现与encoding_translation的问题,所以它似乎是一个问题,只有在使用php_admin_value

+0

这个解决方案部分解决了这个问题:我仍然在调查哪些字符串仍然被mbstring和suhosin混合破坏。 – zekus

0

你尝试在HTML页面上的meta标签下面

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta> 
+0

嗨Roshan,服务器头总是覆盖的HTML元,所以没有必要指定它。无论如何,我也对此进行了测试,问题仍然存在。 – zekus