Tiffany B. Brown

Debugging tip: “Disallowed Key Character” error in CodeIgniter

After 6 hours of massive anxiety, stress, near tears, one pound on my desk, and some hair pulling, I tracked down the source of a nagging Disallowed Key Character error that I received while using CodeIgniter: an extra line break.

The line feed (LF) and carriage return (CR) characters (and their hex code equivalents (%0D and %0A) are forbidden in CodeIgniter’s framework. The hard part is tracking down exactly where that extra line break character lives.

In my case, there was an extra line of blank, barren, not-all-that-obvious white space at the very, very end of one of my controller files, just after the closing ?>.

Related:

28 Responses to “Debugging tip: “Disallowed Key Character” error in CodeIgniter”

  1. iBspoof says:

    Tiffany-

    the "?>" is not required at the end of the line so for Controllers/Models/Libraries you write I would avoid using the ending tag.

  2. iBspoof says:

    Tiffany-

    the "?>" is not required at the end of the line so for Controllers/Models/Libraries you write I would avoid using the ending tag.

  3. tiffany says:

    It’s a force of coding habit, and a good one, IMO. The important thing to remember is that excess white space can cause CodeIgniter to chuck a wobbly.

  4. tiffany says:

    It’s a force of coding habit, and a good one, IMO. The important thing to remember is that excess white space can cause CodeIgniter to chuck a wobbly.

  5. manafta says:

    The extra line break causes PHP to start outputting text (the line break). If your not supposed to output anything before a certain point, any application will fail. This is not codeigniter specific. Learned that the hard way.

  6. manafta says:

    The extra line break causes PHP to start outputting text (the line break). If your not supposed to output anything before a certain point, any application will fail. This is not codeigniter specific. Learned that the hard way.

  7. tiffany says:

    Thank you for pointing that out manafta. I’ve experienced this before with other PHP projects as well.

    CodeIgniter, however, will output a specific “Disallowed Key Characters” error. If you don’t know that line breaks will cause the error, it can be tricky to track down. PHP will usually report that it can’t re-send headers.

    I’ve posted it here so that other CI users can learn about this specific cause and cure.

  8. tiffany says:

    Thank you for pointing that out manafta. I’ve experienced this before with other PHP projects as well.

    CodeIgniter, however, will output a specific “Disallowed Key Characters” error. If you don’t know that line breaks will cause the error, it can be tricky to track down. PHP will usually report that it can’t re-send headers.

    I’ve posted it here so that other CI users can learn about this specific cause and cure.

  9. wenbert says:

    the closing tag ?> is not required. in fact, in Zend Framework, when doing .php files, it is encourage that you avoid it.

  10. wenbert says:

    the closing tag ?> is not required. in fact, in Zend Framework, when doing .php files, it is encourage that you avoid it.

  11. Markus says:

    While we’re on the topic of CI (but a little off your original topic)… Tiffany, your original post on PHP Frameworks really inspired me to take a long hard look at Frameworks, and I wanted to first thank you for opening my eyes. I too decided to go with CI (and have yet to look back). My question is on output techniques. I am curious about how you output data from your models. Currently I pass the entire result set to my view via my controller. I then run a foreach statement in my view to output the results. I have no logical reason why I do this, but something tells me that its not the most efficient and effective way to use CI. What do you think?

  12. Markus says:

    While we’re on the topic of CI (but a little off your original topic)… Tiffany, your original post on PHP Frameworks really inspired me to take a long hard look at Frameworks, and I wanted to first thank you for opening my eyes. I too decided to go with CI (and have yet to look back). My question is on output techniques. I am curious about how you output data from your models. Currently I pass the entire result set to my view via my controller. I then run a foreach statement in my view to output the results. I have no logical reason why I do this, but something tells me that its not the most efficient and effective way to use CI. What do you think?

  13. Ben Ramsey says:

    Some others have already mentioned that the ?> closing tag is not required, but I wanted to reiterate the point. In fact, as you have found, it can cause errors and unintended effects, particularly in library code (such as in a framework), because stray characters (line breaks, spaces, etc.) after the ?> are echoed immediately. Thus, any code that sets a header later in the code (i.e. cookies, etc.) ends up being broken because headers cannot be set after content has been sent (echoed) to the client.

    For this reason, it’s a good practice to leave out the trailing ?> closing tag when writing anything that’s not specifically a view/template.

    You can get around this, though, by turning on output buffering. :-)

  14. Ben Ramsey says:

    Some others have already mentioned that the ?> closing tag is not required, but I wanted to reiterate the point. In fact, as you have found, it can cause errors and unintended effects, particularly in library code (such as in a framework), because stray characters (line breaks, spaces, etc.) after the ?> are echoed immediately. Thus, any code that sets a header later in the code (i.e. cookies, etc.) ends up being broken because headers cannot be set after content has been sent (echoed) to the client.

    For this reason, it’s a good practice to leave out the trailing ?> closing tag when writing anything that’s not specifically a view/template.

    You can get around this, though, by turning on output buffering. :-)

  15. tiffany says:

    @ben: but but… i like the ?>. actually, if i unlearn the habit, i’d probably wind up with the converse problem. i’m weird like that.

    still, dropping the ?> is a good way to prevent the problem. thanks for reiterating the point.

    @markus: in CI, each key name in the data array becomes a variable. $data['foo'] in your controller becomes $foo in your view. using a foreach loop seems unnecessary to me, unless you’re returning multiple database rows.

  16. tiffany says:

    @ben: but but… i like the ?>. actually, if i unlearn the habit, i’d probably wind up with the converse problem. i’m weird like that.

    still, dropping the ?> is a good way to prevent the problem. thanks for reiterating the point.

    @markus: in CI, each key name in the data array becomes a variable. $data['foo'] in your controller becomes $foo in your view. using a foreach loop seems unnecessary to me, unless you’re returning multiple database rows.

  17. Everett says:

    It's not just CodeIgniter… this is a "feature" of PHP. It's best to leave off the closing tag unless it's required.

  18. Everett says:

    It's not just CodeIgniter… this is a "feature" of PHP. It's best to leave off the closing tag unless it's required.

  19. Thanks for restating what iBSpoof, manafta, and Ben all said above. White space errors are not CodeIgniter specific. PHP will display a "Cannot send header" error.

    "Disallowed Key Character," however, IS the specific language CodeIgniter uses. That, in my opinion isn't as clear or familiar as the PHP error and can trip up users new to both. That's what this post is addressing.

  20. Thanks for restating what iBSpoof, manafta, and Ben all said above. White space errors are not CodeIgniter specific. PHP will display a "Cannot send header" error.

    "Disallowed Key Character," however, IS the specific language CodeIgniter uses. That, in my opinion isn't as clear or familiar as the PHP error and can trip up users new to both. That's what this post is addressing.

  21. anon says:

    I have experienced this error as well when I am in a hurry and forget to close my form tag

  22. anon says:

    I have experienced this error as well when I am in a hurry and forget to close my form tag

  23. Vlad says:

    WOW………..anyone here talk English :-?, like what button do I press to get rid off the annoying “Disallowed Key Characters” there is obviously a solution, I just haven't found one in English yet, only in ITist
    I get it while replying on Mercatornet, and that seem to be the only sight.

  24. tiffanybbrown says:

    Vlad, delete *anything* after the closing ?> — including white space and line breaks — or remove the ?>.

  25. Glen Barnhardt says:

    Hi Tiffany,

    This error drove me nuts for quite some time. Even your suggestion didn't fix the problem. Here is what I did to find the issue.

    In the core CI code in system/libraries is a file called input.php I made a small modification to that file so that it would show the actual data in question.

    Around line number 199

    function _clean_input_keys($str)
    {
    if ( ! preg_match(“/^[a-z0-9:_/-]+$/i”, $str))
    {
    exit('Disallowed Key Characters: '.$str); // Added the variable to display.
    }

    return $str;
    }

    I certainly hope this helps others.

  26. kalodont says:

    This might be a bit off-topic, but might help someone seeking a solution for “Disallowed Key Character” in CI. The error may be also triggered by sending POST form data with POST array key names that contain “non-english” characters – I mean leters like ó or Å„.

  27. matthewfedak says:

    I just experienced this as I had a file name and was missing the first quote.

  28. NexusRex says:

    I am also getting this when I have non-english character “ö” in the URI. Anybody know a fix for that?