headers already sent --help needed [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

headers already sent --help needed [solved]

Post 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
Last edited by pthomas on Thu Jun 16, 2005 10:29 am, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post 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
Post Reply