...

View Full Version : PHP Cannot modify headers information - headers already sent



alex57
09-03-2007, 08:57 PM
This script below is designed to write as a string a value taken from a database over an image. However the following message appears.

Cannot modify header information - headers already sent

How can I get this script to work properly. thanks in advance.


$dte = gmdate('Y-m-d');
mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db("info") or die(mysql_error());

$result2 = mysql_query("SELECT * FROM schedule WHERE ('$dte' < EndDate) AND ('$dte' > StartDate)");
$row = mysql_fetch_array($result2);
$CurrentLoc = $row['Location'];

$im = imagecreatefromgif('images/title1024_2_08.gif');
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 211, 211, 211);

imagestring($im, 3, 156, 5, $CurrentLoc, $textcolor);
header("Content-type: image/png");
$img = imagepng($im);

Mwnciau
09-03-2007, 09:05 PM
header() must be before any other content in the page, even a space or a new line.

alex57
09-03-2007, 10:51 PM
Ah yes, the headers error has gone however it still does not work. The image has a cross in the corner as if it cant be located even though the reference is fine. It doesnt seem to like the database query because the script can display any string which hasnt come from the database fine with the image.

Any ideas what could be wrong ?



header("Content-type: image/png");

$dte = gmdate('Y-m-d');
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db("info") or die(mysql_error());

$result2 = mysql_query("SELECT * FROM schedule WHERE ('$dte' < EndDate) AND ('$dte' > StartDate)");
$row = mysql_fetch_array($result2);
$CurrentLoc = $row['Location'];

$im = imagecreatefromgif('images/title1024_2_08.gif');
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 211, 211, 211);

imagestring($im, 3, 156, 5, $CurrentLoc, $textcolor);
$img = imagepng($im);

Mwnciau
09-03-2007, 10:59 PM
Try replacing $img = imagepng($im); with echo mysql_error() and see what it says

alex57
09-04-2007, 11:37 AM
With
echo mysql_error() after
$img = imagepng($im); there is no error, the graphic is not there, just a box with a red cross. With
echo mysql_error() instead of
$img = imagepng($im); there is still no error message and the graphic wit a red cross doesnt appear either.

rafiki
09-04-2007, 07:14 PM
you should edit your posts it contains sensitive information ask a mod if it wont let you

meth
09-05-2007, 01:04 AM
Ensure all errors from php are displaying. I imagine a function is returning a warning or error since there's a few places in your code where errors may occur.

Also make sure the image file is readable to everyone.

I'd change your db pass if I were you; chances are that this page has been cached by a SE by now.

CFMaBiSmAd
09-05-2007, 02:26 AM
Because you are outputting a content type header for an image, any error messages or echoed text that php might output to the browser won't easily be seen.

Also, turn on full php error reporting and/or check your web server log for errors to find out if any of the function calls are failing.

I recommend commenting out the header(...) and imagepng(...) statements so that you can see any PHP/mysql generated errors. Also, add an or die(mysql_error()); to your mysql_query(...) statement to get any feedback if the query is failing. Then, once you have the code working, put back in the header(...) and imagepng(...) statements.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum