CodeIgniter Perfect Solution URL contains Chinese string _php tips

Source: Internet
Author: User
Tags urlencode codeigniter

CodeIgniter default configuration is not allowed to include non-ASCII characters in the URL, if the URL contains non-ASCII characters, then CI will not be polite to throw errors. This article to the Code Agriculture introduction CodeIgniter How to solve the URL contains the Chinese string.

You might say, then I urlencode this URL using a function? No way. Because Web server automatically decode a URL after it receives a urlencode, and then converts those strings in PHP to what he originally represented, and uses the Web server's own URL-coded character set (IIS6 Chinese version is GBK, Apache 2.2 is UTF-8) to the application, which makes the URL of CI is already a decoded, whether you have a URL to UrlEncode, the browser will automatically detect the request, if not, it will automatically encode. Therefore, manual urlencode does not solve the problem. So what should we do to solve the problem?

For the CI framework, the current point of view is to try not to modify it, but to extend it, CI provides a good extension mechanism, we only need to be under Application/core/(2.0 version of the previous application/libraries/ ) Add a file my_uri.php, which reads:

<?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 has disallowed characters. ') 
   $str = UrlDecode ($STR); 
  } 
  return $str; 
 }  

I covered the _filter_uri method in the original Ci_uri so that the Chinese URL could be detected. However, if there are spaces in the URL, also die, how to do? Originally, UrlEncode will convert the space into +, and the default configuration of CI is not allowed + appear in the URL, OK, put

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

Change into

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

It's OK.

Or

The first step is to put the config.php

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

Replace into

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

All the operations we have done, but then you may have encountered a new problem, that is the URL to get the Chinese information is garbled, do not know if your server will not encounter this problem, but I encountered (IIS). But local is normal, local use is Apache.

OK, I am in the program, the $_server[' Request_uri '] print out, found that it is garbled, think hard, this is how to return? This means that before I get the parameters in the URI, it's already coded, OK, we use Iconv to decode:

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

Now print out to see, well, the original Chinese parameters printed out, is correct.

This problem has been solved, but still have a question, why is the URL gb2312 encoded, if use in my program (I am using utf-8 code), also need to convert to Utf-8 code, is not the Web server is related to it, I hope you can help answer.

The above CodeIgniter perfect solution URL contains the Chinese string is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.