Fixed: “The URI you submitted has disallowed characters.” error CodeIgniter

I started up a development project today after upgrading to snow leopard, and none of the codeigniter links worked.  they all said “The URI you submitted has disallowed characters.”  Why?  This hadnt happened before, same project what changed?

Snow leopard upgraded my php dev environment to 5.3 from 5.2.6  And a few things have changed since then.  Namely php bug #47229 “preg_quote should escape “-” (minus) as well” was fixed. (technically in 5.2.8)  CodeIgniter checks uri for allowed characters to prevent some bad things.  But the use preg_quote to convert the allowed list of character to something usable in a regular expression.  Now the minus “-”, or I’d call it a dash (but I know there is a longer character for that)  gets escaped in preg_quote with a backslash “\”.  That cause the expression “a-z 0-9″ to be converted to “a\-z 0\-9″ which will not work in a regex.

How to fix it. (assuming codeigniter 1.7)

1) in codeigiter system/libraries open  URI.php  line 189 you’ll find

if ( ! preg_match("|^[".preg_quote($this->config->item('permitted_uri_chars'))."]+$|i", rawurlencode($str)))

Change that to:

if ( ! preg_match("|^[".($this->config->item('permitted_uri_chars'))."]+$|i", rawurlencode($str)))

Note we removed the preg_quote().  Now in your system/application/config/config.php file  look for line 126 (unless you’ve added a lot to you config will be around there somewhere)

Change the line

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

to:

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

we’re now preparing our allowed character string in the config file and skipping preg_quote.  And that’s it.  Now your uri should work

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

187 Responses to “Fixed: “The URI you submitted has disallowed characters.” error CodeIgniter”

  1. Rodrigo says:

    You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

  2. Эээ… Прочитал весь пост, о общем-то есть кое-что новое, однако, если честно не особо интересно. Жалко, что редко постите. Зато качественно. Попробуйте писать конкретнее, размыто получается.

  3. +1 к предыдущему комменту :)

  4. Публий Вергилий Марон says:

    Меня дело касается, когда горит стена у соседа.

  5. get found says:

    get found…

    Really gained insight from this post, thanks for taking time to write it…

  6. sykotic says:

    sykotic…

    I found a lot of great points in this post, nice…

  7. dhunter65 says:

    You rock! That would have taken me forever to figure out on my own.

  8. [...] via Fixed: “The URI you submitted has disallowed characters.” error CodeIgniter | DavidMichaelThomps…. [...]

  9. tmelvin says:

    You saved me hours!

  10. Fabian says:

    Legend mate! Thanks a lot.

  11. Anuraag says:

    Thanks very much for this informative fix.

  12. aankun says:

    so helpful thanks a lot ^_^

  13. Jason says:

    Excellent, thanks!

  14. darelparker says:

    Had this problem with several sites after moving accounts to a new server. Your solution fixed the problem. Thanks!

  15. blogregator says:

    Very useful article. Thank you!

  16. dlsb says:

    Great post! I fix my problem with OpenID! Thanks!

  17. pkakajohn says:

    you are the best man in the world..
    That works fine to me..

    php 5.3.0
    codeigniter 1.7.2

  18. webmaster says:

    Very usefull. Thanks. U r a livesaver

  19. anon says:

    Thanks, fixed my problem immediately

Leave a Reply

You must be logged in to post a comment.