Tiffany B. Brown

a mish-mosh of stuff

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:

  • http://www.bleakworld.com/ iBspoof

    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.

  • http://www.bleakworld.com iBspoof

    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.

  • tiffany

    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.

  • tiffany

    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.

  • manafta

    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.

  • manafta

    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.

  • tiffany

    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.

  • tiffany

    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.

  • http://blog.ekini.net/ wenbert

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

  • http://blog.ekini.net wenbert

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

  • http://www.blackweb20.com/ Markus

    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?

  • http://www.blackweb20.com Markus

    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?

  • http://benramsey.com/ Ben Ramsey

    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. :-)

  • http://benramsey.com/ Ben Ramsey

    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. :-)

  • tiffany

    @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.

  • tiffany

    @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.

  • Everett

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

  • Everett

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

  • http://intensedebate.com/people/tiffanybbrown tiffanybbrown

    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.

  • http://intensedebate.com/people/tiffanybbrown tiffanybbrown

    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.

  • anon

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

  • anon

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

  • Vlad

    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.

  • tiffanybbrown

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

  • Glen Barnhardt

    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.

  • kalodont

    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 ń.

  • http://twitter.com/matthewfedak matthewfedak

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

  • NexusRex

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