How can CodeIgniter solve the problem that the URL contains a Chinese string? codeigniterurl

Source: Internet
Author: User

How can CodeIgniter solve the problem that 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:

<? 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 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?

Related Article

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.