|
我查了很多资料,发现大侠们的解决方案都有一些瑕疵。(也可能是生产和开发环境不同导致的问题。)先将我的终极解决方案告知你。
1,在 config.php 里面修改
PHP复制代码 $config['permitted_uri_chars'] = '|^[a-z 0-9~%\.\:_\+\- \x{4e00}-\x{9fa5}]+$|iu'; 复制代码
2, 在 application/core 里创建新的类,扩展uri核心类,文件名为 MY_URI.php
内容为
PHP复制代码
class MY_URI extends CI_URI {
function _filter_uri ($str) {
$encoding = mb_detect_encoding($str, "gb2312,utf-8");
if ($encoding != "utf-8") {
$str = iconv($encoding, "utf-8", $str);
}
if ($str != '' && $this->config->item('permitted_uri_chars') != '' && $this->config->item('enable_query_strings') == FALSE) {
// preg_quote() in PHP 5.3 escapes -, so the str_replace() and addition of - to preg_quote() is to maintain backwards
// compatibility as many are unaware of how characters in the permitted_uri_chars will be parsed as a regex pattern
if (!preg_match($this->config->item('permitted_uri_chars'), $str)) {
show_error ('myurl:The URI you submitted has disallowed characters.' . $str, 400);
}
}
// Convert programatic characters to entities
$bad = array('$', '(', ')', '%28', '%29');
$good = array('$', '(', ')', '(', ')');
return str_replace($bad, $good, $str);
}
}
复制代码
这里先判断了传输的内容的编码,如果不是utf8则转换成utf8,然后再匹配正则。
目前我本机测试环境是 windows 8 + iis7 测试通过。生产环境为nginx 待测试。 |
|