CodeIgniter Perfect Solution URL contains Chinese string, Codeigniterurl
CodeIgniter The default configuration is not allowed to include non-ASCII characters in the URL, if the URL contains non-ASCII characters, then CI will be polite to throw an error. This article introduces CodeIgniter how to resolve URLs that contain Chinese strings.
You might say, I urlencode this URL with a function? No way. Because Web server automatically decode it after it receives a UrlEncode URL, and then translates those strings in PHP into what he originally meant, and uses the Web server's own URL encoding character set (IIS6 Chinese is GBK, Apache 2.2 is UTF-8) sent to the application, which makes the URL of the CI is already a decoded, whether you do not urlencode the URL, the browser will make the request automatically detect, if not, it will be automatically encoded. Therefore, manual urlencode does not solve the problem. So what should we do to solve this problem?
For the CI this framework, used to now, my point is to try not to modify it, but to expand him, CI provides a good extension mechanism, we only need under the Application/core/(2.0 version before the application/libraries/ ) Add a file my_uri.php with the following content:
<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed '); Class My_uri extends Ci_uri { /** * Custom URL filter Function * * @access Private * @param string * @return Str ing * * 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 have disallowed characters. '); } $str = UrlDecode ($STR); } return $str; } }
I covered the original Ci_uri in the _filter_uri method, so that you can make the Chinese URL through detection. But what if there are spaces in the URL? Originally, UrlEncode will convert the space into +, and the default configuration of CI is not allowed + appear in the URL, OK, put
Change into
You can do it.
Or
The first step is to put the config.php
Replaced by
All the operations we have done, but then you may encounter a new problem, that is the URL to get the Chinese information is garbled, do not know if you will encounter the problem on the server, but I encountered (IIS). But local is normal, local use is Apache.
OK, I in the program, put $_server[' Request_uri '] print out, found it is garbled, think, what is this? This means that before I get the parameters in the URI, it has been encoded, well, we use ICONV decoding:
Now print out to see, OK, the original Chinese parameters printed out, is correct.
This problem solved, but there is a question, why is the URL gb2312 encoded, if in my program (I use UTF-8 encoding), but also need to convert to UTF-8 encoding, is not related to the Web server, I hope you can help answer.
The above CodeIgniter the perfect solution URL contains Chinese string is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.
http://www.bkjia.com/PHPjc/1127918.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127918.html techarticle codeigniter Perfect Solution URL contains Chinese strings, Codeigniterurl codeigniter default configuration is not allowed to include non-ASCII URLs in the URL, if the URL contains non-ASCII characters, then CI will ...