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

url中的中文问题

[复制链接]
发表于 2012-11-22 15:52:53 | 显示全部楼层 |阅读模式
我现在做一个搜索页面,然后用户输入的关键词保存在路径中,现在的问题是路径中是不允许中文的,我的url如这个    http://127.0.0.2/index.php/search/第八/2,第八是关键词,2是第几页的意思,报的错是
An Error Was Encountered

The URI you submitted has disallowed characters.


就是这个错误,
我采用的方案,1,通过urlencode,将中文编码,但是这个之所以失败,是因为浏览器会自动urldecode,就是转换回来,成为中文,失败,
2,base64_encode,也是失败了,因为他对数字也编码了,
3,
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=11594
根据这个帖子的1,3楼的内容,我在system/core中添加了一个文件,文件是AST_Uri.php,因为config.php中的$config['subclass_prefix'] = 'AST_';
但是结果还是报错
然后,我就山穷水尽了,求助。。怎么解决这个路径问题呢
发表于 2013-3-8 18:53:55 | 显示全部楼层

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

本帖最后由 gogogm 于 2013-3-8 18:57 编辑

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

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 待测试。
发表于 2014-12-19 21:09:03 | 显示全部楼层
gogogm 发表于 2013-3-8 18:53
**** 作者被禁止或删除 内容自动屏蔽 ****

这种方法中文倒是没问题,但是PHP的 imagejpeg 这个自带函数不能使用了。。。我要用到这个命令,发现不能输出图片,不做这个配置就可以。但是不做这个配置,中文又不能带在链接中

本版积分规则