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 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 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
$config [' permitted_uri_chars '] = ' A-Z 0-9~%.:_\+\-';
You can do it.
Or
The first step is to put the config.php
$config [' permitted_uri_chars '] = ' A-Z 0-9~%.:_\-';
Replaced by
$config [' permitted_uri_chars '] = ' A-Z 0-9~%.:_-u4e00-u9fa5 ';
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:
Iconv ("gb2312", "UTF-8", $uri);
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.
Original address: http://www.manongjc.com/article/808.html
CodeIgniter Related reading:
- CodeIgniter URL
- CodeIgniter Controller
- CodeIgniter Reserved Name
- CodeIgniter View
- CodeIgniter model
- CodeIgniter Auxiliary functions
- CodeIgniter Class Library
- CodeIgniter Creating a class library
- CodeIgniter Drive
- CodeIgniter Creating a drive
- CodeIgniter Core Class
- CodeIgniter Affiliated Class
- CodeIgniter Hooks
- CodeIgniter Automatic loading
- CodeIgniter public functions
- CodeIgniter compatibility function
- CodeIgniter URI Routing
- CodeIgniter Error Handling
- CodeIgniter Web Cache
- CodeIgniter Analyzer Class
- CodeIgniter CLI mode operation
- CodeIgniter Management Application
- CodeIgniter Handling Multi-environment
- CodeIgniter substitution syntax
- CodeIgniter Safety
- CodeIgniter Development Code
- CodeIgniter Routing (URL) optimization analysis and explanation
- How the CodeIgniter framework removes index.php from the URL
CodeIgniter How to resolve URLs that contain Chinese strings