用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2832|回复: 3
收起左侧

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

[复制链接]
发表于 2013-3-8 18:58:51 | 显示全部楼层 |阅读模式

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

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 待测试。
发表于 2013-3-8 21:56:07 | 显示全部楼层
哪有这么复杂。。。传之前先编码,接收到了再解码就行了。。。。
发表于 2013-3-9 10:14:25 | 显示全部楼层
跟屁虫 发表于 2013-3-8 21:56
哪有这么复杂。。。传之前先编码,接收到了再解码就行了。。。。

楼上正解,而且更安全
发表于 2013-11-9 11:37:26 | 显示全部楼层
我的内容页 老跳首页 咋办啊   http://it360.org.cn/view/2215  帮看看啊 谢谢啦

本版积分规则