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
















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.
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.
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.
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.
the closing tag ?> is not required. in fact, in Zend Framework, when doing .php files, it is encourage that you avoid it.
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?
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.
@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.