PHP header error messages
This drove me crazy for about six hours straight. And yes, I felt like an idiot when I figured things out. Here’s how not to let it drive you batty.
The scenario
I was working on a survey for my employer, and was trying to send a cookie to the user to “prevent” — although it’s more like erect a small stumbling block than a preventive measure — people from taking the survey more than once.
The problem
Now sending a cookie is easy enough. But if you don’t get things just right, you will — as I did — get an error that’s a bit like this one:
Warning: Cannot modify header information — headers already sent by (output started at /usr/www/content/www.yourserver.edu/html/survey/lib.php:201) in /usr/www/content/www.yourserver.edu/html/survey/lib.php on line 6
WTF? And more importantly, where was the error?
One of the cool things about PHP is that it tells you where your boo-boos are. In this case, the stuff in parentheses tells you where your output begins. The :201 means that the error is on line 201 of lib.php. The next portion tells you the header-sending request is on line 6.
What’s output? Output is anything — text, html, even line breaks and spaces — that is sent back to the client before the cookie or header.
Yes, that’s right. Line breaks and spaces are output. And as it turns out, the problem that plagued me for the better part of a day was an extra line break on line 201.