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:

  • 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 ?>.
  • anon
    I have experienced this error as well when I am in a hurry and forget to close my form tag
  • 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.
  • 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.
  • 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.
  • 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. :-)
  • 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?
  • the closing tag ?> is not required. in fact, in Zend Framework, when doing .php files, it is encourage that you avoid it.
  • 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.
  • 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
    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-

    the "?>" is not required at the end of the line so for Controllers/Models/Libraries you write I would avoid using the ending tag.
blog comments powered by Disqus
previous post: Who do you trust more: Corporations or Government?
next post: Mike Gravel vs. Obama: “Juneau What? Anchorage What?”