The default configuration of codeIgniter does not allow the URL to contain non-ASCII characters. If the URL contains non-ASCII characters, then CI will throw errors unceremoniously. This article introduces coders to how CodeIgniter solves the problem of URLs containing Chinese strings.
You may say, what if I use the function urlencode on this URL? no. Because the Web Server will automatically decode a urlencoded URL after receiving it, and then convert the strings obtained in PHP into the meaning it originally represents, and use the Web Server's own URL encoding character set (IIS6 Chinese version is GBK, Apache 2.2 is UTF-8) is transmitted to the application, which makes the URL obtained by CI already decoded. Regardless of whether you have urlencoded the URL, the browser will automatically detect it when making a request. If not , the encoding will be performed automatically. Therefore, manually urlencoding does not solve the problem. So what should we do to solve this problem?
For a framework like CI, my point of view is to try not to modify it, but to extend it. CI provides a good extension mechanism. We only need to install it under application/core/ (version 2.0 The previous one was application/libraries/) and added a file MY_URI.php with the content:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_URI extends CI_URI { /** * 自定义的url过滤函数 * * @access private * @param string * @return string */ function _filter_uri($str) { if ($str != '' AND $this->config->item('permitted_uri_chars') != '') { $str = urlencode($str); if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", $str)) { exit('The URI you submitted has disallowed characters.'); } $str = urldecode($str); } return $str; } }
I overwrote the _filter_uri method in the original CI_URI, so that the Chinese URL can pass the detection. However, if there are spaces in the URL, it won't work. What should I do? It turns out that urlencode will convert spaces into , and the default configuration of CI does not allow it to appear in the URL. OK, put
$config ['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
changed to
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\+\-';
That’s it.
or
The first step is to change the
in config.php$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
with
$config['permitted_uri_chars'] ='a-z 0-9~%.:_-u4e00-u9fa5';
We have completed all the above operations, but you may encounter a new problem next, that is, the Chinese information obtained in the URL is garbled. I don’t know if you will encounter this problem on your server. But I encountered (IIS). But it is normal locally, and apache is used locally.
Okay, I printed out $_SERVER['REQUEST_URI'] in the program and found that it was garbled. I was thinking hard, what is going on? This means that before I get the parameters in the uri, they have been encoded. Well, we use iconv to decode:
iconv("gb2312","UTF-8",$uri);
Now print it out and see. Okay, the original Chinese parameters are printed out and they are correct.
This problem is solved, but there is still a question, why is the url encoded in gb2312? If it is used in my program (I use utf-8 encoding), it also needs to be converted to utf-8 encoding, yes It's not related to the web server. I hope you can help answer it.
Original address: http://www.manongjc.com/article/808.html
codeIgniter related reading: