gogogm 发表于 2013-3-8 18:58:51

codeigniter url 传输中文报错的终极解决之道


我查了很多资料,发现大侠们的解决方案都有一些瑕疵。(也可能是生产和开发环境不同导致的问题。)先将我的终极解决方案告知你。

1,在 config.php 里面修改
$config['permitted_uri_chars'] = '|^+$|iu';

2, 在 application/core 里创建新的类,扩展uri核心类,文件名为 MY_URI.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 待测试。

跟屁虫 发表于 2013-3-8 21:56:07

哪有这么复杂。。。传之前先编码,接收到了再解码就行了。。。。

Altair 发表于 2013-3-9 10:14:25

跟屁虫 发表于 2013-3-8 21:56 static/image/common/back.gif
哪有这么复杂。。。传之前先编码,接收到了再解码就行了。。。。

楼上正解,而且更安全

灵魂工程师_² 发表于 2013-11-9 11:37:26

我的内容页 老跳首页 咋办啊   http://it360.org.cn/view/2215帮看看啊 谢谢啦
页: [1]
查看完整版本: codeigniter url 传输中文报错的终极解决之道