📜 ⬆️ ⬇️

Solving the problem with Russian characters in the URL

It is known that by default in Code Igniter, Russian letters in addresses are prohibited.
However, even if you change the lines in the system / application / config / config.php file
$ config ['permitted_uri_chars'] = 'az 0-9 ~%.: _ \ -';
on
$ config ['permitted_uri_chars'] = 'a-za-yo 0-9 ~%.: _ \ -';
The problem did not dare.
I had to look into the file system / libraries / URI.php, which is responsible for processing addresses. The decision on the correctness of the URL segment is dealt with by this line (the _filter_uri method):
if ( ! preg_match( "|^[" .preg_quote($ this ->config->item( 'permitted_uri_chars' )). "]+$|i" , $str)) { ... } * This source code was highlighted with Source Code Highlighter .
  1. if ( ! preg_match( "|^[" .preg_quote($ this ->config->item( 'permitted_uri_chars' )). "]+$|i" , $str)) { ... } * This source code was highlighted with Source Code Highlighter .
  2. if ( ! preg_match( "|^[" .preg_quote($ this ->config->item( 'permitted_uri_chars' )). "]+$|i" , $str)) { ... } * This source code was highlighted with Source Code Highlighter .
if ( ! preg_match( "|^[" .preg_quote($ this ->config->item( 'permitted_uri_chars' )). "]+$|i" , $str)) { ... } * This source code was highlighted with Source Code Highlighter .

After half an hour of googling, it became clear that in order to make preg_match accept Unicode characters, you need to add the 'u' modifier to the regular expression. After that, everything worked.

I publish this way of solving the problem, and I am interested in blog readers: is there any way around this problem without modifying the class from the core framework?

UPD:
while I was publishing, it dawned on me that you can not change the system class, but expand it by overloading the _filter_uri method. To do this, create the file system / application / libraries / MY_URI.php and place the following code there:
  1. <? php
  2. class MY_URI extends CI_URI
  3. {
  4. function _filter_uri ($ str)
  5. {
  6. if ($ str! = '' AND $ this -> config-> item ( 'permitted_uri_chars' )! = '' )
  7. {
  8. if (! preg_match ( "| ^ [" .preg_quote ($ this -> config-> item ( 'permitted_uri_chars' )). "] + $ | ui" , $ str))
  9. {
  10. exit ( 'The URI you submitted has disallowed characters.' );
  11. }
  12. }
  13. return $ str;
  14. }
  15. }
* This source code was highlighted with Source Code Highlighter .

')

Source: https://habr.com/ru/post/40465/


All Articles