Page 1 of 1

headers already sent --help needed [solved]

Posted: Thu Jun 16, 2005 10:11 am
by pthomas
Alright, I'm completley clueless here. I've been trying to get this class up and running for the last day or two and I'm now to the point where I'm getting a
headers already sent
message. I know it happens when you print something before you sene dthe headers, however, I'm not creating any html. Please help, I'm stumped.

The calling file that gives me the error:

Code: Select all

<?php
include ("text2image-class.php");

$image = new Text2Image();
$image->Set_Text ( "print me" );
$image->Create_Image();
$image->Show_Image();
?>
Here's the only function that returns any header info, or prints anything inside my class text2image-class.php

Code: Select all

function Show_Image()
{ 				
	header("Content-type: image/png");		//Set the header
	imagePNG($this->Image);					//Send the image to the browser
	imageDestroy($this->Image);				//throw away our canvas
}
Note: I do have the Set_Text, Create_Image and Show_Image functions returning true on success and false otherwise.

Why am I getting this error?:
Warning: Cannot modify header information - headers already sent by
(output started at /<dir to website>/text2image-class.php:507) in
/<dir to website>/text2image-class.php on line 502
Thanks,
Paul

Posted: Thu Jun 16, 2005 10:22 am
by patrikG

Posted: Thu Jun 16, 2005 10:27 am
by pthomas
Here's something interesting, I commented out the 3 lines in the Show_Image function and just printed a single line of text and I get somehting interesting.

Code: Select all

function Show_Image()
{
	echo "timmy";
//	header("Content-type: image/png");		//Set the header
//	imagePNG($this->Image);				//Send the image to the browser
//	imageDestroy($this->Image);				//throw away our canvas
}
This results in printing out this:
timmy EDIT -> just imagine that there is a tab whitespace in front ;p
Notice a problem? It should have printed out
timmy
Somehow whitespace is getting sent into the HTML. I ended up tracking this down and gave one big WTF!? The problem is cause by having a tab or other whitespace after the class definition's ending '?>'. I removed the extra whitespace after the '?>' in my text2image-class.php file and I no longer get the error and my picture shows up just fine.

Problem solve.....f----in whitespace!
Paul