Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts

    PHP Cannot modify headers information - headers already sent

    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.

    PHP Code:
    $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($im255255255); 
    $textcolor imagecolorallocate($im211211211); 

    imagestring($im31565$CurrentLoc$textcolor); 
    header("Content-type: image/png"); 
    $img imagepng($im); 
    Last edited by firepages; 09-05-2007 at 01:04 AM.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    header() must be before any other content in the page, even a space or a new line.

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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 ?

    PHP Code:
    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($im255255255);  
    $textcolor imagecolorallocate($im211211211);  

    imagestring($im31565$CurrentLoc$textcolor);    
    $img imagepng($im); 
    Last edited by firepages; 09-05-2007 at 01:05 AM.

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Try replacing $img = imagepng($im); with echo mysql_error() and see what it says

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Posts
    216
    Thanks
    9
    Thanked 0 Times in 0 Posts
    With
    PHP Code:
    echo mysql_error() 
    after
    PHP Code:
    $img imagepng($im); 
    there is no error, the graphic is not there, just a box with a red cross. With
    PHP Code:
    echo mysql_error() 
    instead of
    PHP Code:
    $img imagepng($im); 
    there is still no error message and the graphic wit a red cross doesnt appear either.

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you should edit your posts it contains sensitive information ask a mod if it wont let you

  • #7
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    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.
    Last edited by meth; 09-05-2007 at 12:06 AM. Reason: page cache warning
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,036
    Thanks
    2
    Thanked 316 Times in 308 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •