How to solve the problem of CodeIgniter URL containing Chinese Strings

Source: Internet
Author: User
How can CodeIgniter solve the problem that the URL contains a Chinese string, codeigniterurl. How can CodeIgniter solve the problem that the URL contains a Chinese string? by default, codeigniterurlcodeIgniter does not allow the URL to contain non-ASCII characters. if the URL contains non-ASCII characters, then CI will address how CodeIgniter solves the problem where the URL contains a Chinese string, codeigniterurl

By default, codeIgniter does not allow the URL to contain non-ASCII characters. if the URL contains non-ASCII characters, CI will throw an error politely. This article describes CodeIgniter's solution to URL containing Chinese strings.

You may say, what if I use the urlencode function for this URL? No. Because the Web Server will automatically decode the URL after receiving it, and convert the strings obtained in PHP to their original meaning, and use the Web Server's own URL encoding character set (IIS6 Chinese version is GBK, Apache 2.2 is a UTF-8) sent to the application, which makes the ci url has been decoded, whether or not you perform urlencode on the URL, the browser automatically detects the URL when sending the request. if not, the URL is automatically encoded. Therefore, manual urlencode cannot solve the problem. So how should we solve this problem?

For the CI framework, my point is to try not to modify it, but to expand it. CI provides a good extension mechanism, we only need to add a file named MY_URI.php under application/core/(application/libraries/before version 2.0) with the following content:

 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 overwrite the _ filter_uri method in the original CI_URI so that the Chinese URL can pass the detection. However, what should I do if there is space in the URL? Originally, urlencode will convert the space to +, and the default configuration of CI does not allow + to appear in the URL. OK

$config ['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; 

Change

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\+\-';  

You can.

Or

Step 1: Set

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';    

Replace

$config['permitted_uri_chars'] ='a-z 0-9~%.:_-u4e00-u9fa5';    

We have done all the operations, but next you may encounter a new problem, that is, the Chinese information in the url is garbled, I don't know if this problem will occur on your server, but I have encountered (IIS ). However, it is normal locally, and apache is used locally.

Well, I printed $ _ SERVER ['request _ URI '] in the program and found it was garbled. I thought for a while. what is the problem? This indicates that the uri has been encoded before I get the parameter. well, we use iconv decoding:

iconv("gb2312","UTF-8",$uri);

Print it out now. well, the original Chinese parameters are printed out, which is correct.

This problem has been solved, but I still have 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. is it related to the web server? I hope you can help me solve this problem.

Address: http://www.manongjc.com/article/808.html

CodeIgniter:

  • CodeIgniter URL
  • CodeIgniter controller
  • CodeIgniter reserved name
  • CodeIgniter view
  • CodeIgniter model
  • CodeIgniter auxiliary functions
  • CodeIgniter class library
  • CodeIgniter creates a class library
  • CodeIgniter driver
  • CodeIgniter create a drive
  • CodeIgniter core class
  • CodeIgniter ancillary class
  • CodeIgniter hook
  • CodeIgniter automatic loading
  • CodeIgniter common functions
  • CodeIgniter compatibility functions
  • CodeIgniter URI routing
  • CodeIgniter error handling
  • CodeIgniter webpage cache
  • CodeIgniter analyzer class
  • CodeIgniter CLI running
  • CodeIgniter management application
  • CodeIgniter processes multiple environments
  • CodeIgniter substitution syntax
  • CodeIgniter security
  • CodeIgniter development specifications
  • Analysis and explanation of codeigniter URL optimization
  • How does the CodeIgniter framework remove index. php from the url?

Http://www.bkjia.com/PHPjc/1125999.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125999.htmlTechArticleCodeIgniter how to solve the URL contains a Chinese string, codeigniterurl codeIgniter default configuration is not allowed in the URL contains non-ASCII characters, if the URL contains non-ASCII characters, then CI will...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.