Code Czar

Daryl Joseph Ducharme,

Headers already sent?!

, , ,

In my experience, the most annoying and hard to track down bugs are the simplest things. Sometimes you misspell a word. Sometimes you accidentally check for a boolean true when you should be checking for false. The worst are misplaced spaces, tabs, carriage returns and other whitespace that makes an bug hard to track down.

What I was trying to do was simple. I just needed to add a class file into my PHP code for an application I am working on. After I added the class file like so:
require_once( "../../../fileDirectory/my_necessary_class.php" );
I started to get errors.

Because this was a Flex application using AMFPHP, my first error popped up in the service browser.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at RawAmfService/internal::readData()
	at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
	at flash.events::EventDispatcher/dispatchEvent()
	at flash.net::URLLoader/flash.net:URLLoader::onComplete()
	at [io]
Ack! Near useless errors, at least it looked like the AMF reading abilities of the flash player were being confounded by whatever they were getting sent. From what eventually came up in the Service Browser's results window I had a bit more information to work with:
(Object)#0
  message = "faultCode:INVALID_AMF_MESSAGE faultString:'Invalid AMF message' faultDetail:'

Warning: Cannot modify header information - headers already sent by (output started at /a/long/path/to/a/site/root/fileDirectory/my_necessary_class.php:464) in /a/long/path/to/a/site/root/amfphp/core/shared/exception/php4Exception.php on line 38
'"
  name = "Error"
  rootCause = (null)

Eureka! proper AMF file headers were not able to be sent because, headers were already sent before the call to send AMF headers.

My deductive process to find the error went like this:
1. Perhaps the file isn't where I think it is. I change the require_once code a little and get an error that tells me that I had it right before.
2. Look for any obvious errors in the class - it works in other programs so I wasn't counting on this to work out for me. It didn't.
3. So that I could edit the class without breaking other applications I made a copy in the same directory and to see if somehow tha was affecting the headers. Still a no go.
4. Finally I decided to comment the entire file out and try it. Success! Slowly I uncommented function by function. In the end I uncommented every function and it still worked.

Not being one to fix a bug without understanding it I did a comparison of the original file and my new version. The only difference was my new version did not have an extra line after the closing ?>. When I saw that I remembered something I read about the PHP include functions( include, include_once, require and require_once ).

When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.



What was happening was that the last line of that file was being sent as HTML, so PHP sent regular HTML headers when the file was included and before the AMF headers were sent. So if you are trying to output non-HTML and your run into a problem where the wrong headers are being sent, check to see if you have some extra whitespace outside of your <?php ?> tags.

----
Daryl "Deacon" Ducharme is currently Director of Application Development for the Interactive Agency Provis Media Group, LLC which helps organizations enhance identity, connect with customers and increase productivity.

Ah, The MemoriesCNN/YouTube Debates

最新コメント

匿名 2008年4月14日月曜日20:35:01

joe! writes: Gosh! I had just exactly the same trouble... You saved me a lot of time. Many thanx.

匿名 2008年10月15日水曜日7:30:39

John writes: Me too! So glad I found this page! Thanks for the detailed write up.

Daryl Joseph Ducharmedarylducharme 2008年10月15日水曜日13:14:25

I like it when I can help people out. Bugs like this suck because you spend a lot of time tracking down the smallest bug. Some would say you learn from the experience, and I did. I think it might be easier if other people learned from mine.

匿名 2009年6月30日火曜日5:44:43

AnonymousFlasher writes: You were the only Google search result, and you saved me a lot of time. Thanks so much!

Daryl Joseph Ducharmedarylducharme 2009年6月30日火曜日13:16:41

Glad to be of assistance. I detest not finding answers to problems like this online. You know someone else must have run into this situation before.

匿名 2009年9月5日土曜日9:37:39

martin writes: thank youuuuuuuuu

匿名 2009年10月6日火曜日3:58:32

Anonymous writes: Same problem. You're the man.

Daryl Joseph Ducharmedarylducharme 2009年10月6日火曜日14:15:06

I wrote this in 2007 and it is still helping people in 2009. I am very happy about that.

匿名 2009年12月26日土曜日1:14:03

Anónimo writes: Goosh men I can' believe this... The error was appearing because I left a blank space after ?> This _

匿名 2010年3月19日金曜日13:29:37

Edward writes: Man do you save my life, still in 2010. Thank you so much for this post. What annoying bug.

匿名 2010年4月7日水曜日13:52:47

Jason Martin writes: Hi, I hope people looking for a response to this find this page and read down, because I had the same issue, unfortunately, I could not find a single bit of extra whitespace. What I tried was: Remove all whitespace from the tags. Didn't work. You can drop the end ?> tag altogether, and sometimes that fixes these issues. We can because: we aren't embedding in HTML, so we don't need the exit tag at all. This however, didn't work. The final solution was: In gateway.php on line 12 " - setLooseMode(bool mode) If true, output buffering is enabled and error_reporting is lowered to circumvent a number of documented NetConnection.BadVersion errors" Edit the file at line 154ish to this: $gateway->setLooseMode(true); $gateway->service();

匿名 2010年7月20日火曜日22:18:40

Felipe Fagua writes: Hey man, tanks a lot, your post was helpful to me. The same mistake happens when you call "echo" function.

匿名 2011年8月2日火曜日9:56:08

Anonymous writes: Its helping people in 2011 . thankyou

引用機能の使い方:

  1. テキストを選択してください
  2. 引用リンクをクリック

コメントを追加

コメント
(BB コードと HTML は匿名ユーザーからのコメントには使用できません。)

もし単語が読めない場合には、小さなリロードアイコンを押してください。


スマイリー