Page 1 of 2

Cannot modify header information

Posted: Wed Jan 17, 2007 11:29 am
by josefv
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi there, all!

Just a supposedly simple question on the header function; I keep on getting the following error in my code:

Cannot modify header information - headers already sent by...

My code is as follows:

Code: Select all

while($row = mysql_fetch_assoc($banners))
                {
                     if($row["Image"] != NULL)
                    {
                         header("Content-type: image/jpeg"); PROBLEM LIES HERE                       
                         echo("<a href=\"" . $row["URL"] . "\" target=\"_blank\">" .
                             "<img src=\""  .$row["Image"]  . "\"></img></a><br>");
                    }
                    else
                    {
                        echo("<a href=\"" . $row["URL"] . "\" target=\"_blank\">" .
                             "<img src=\"" .$row["ImageURL"]  . "\"></img></a><br>");
                    }
                }
I've commented out the echo's above, but my problem clearly lies with the header call. The script is supposed to display a BLOB image from a mySql DB, but I don't understand what I'm doing wrong with the header.

Your help is highly appreciated!

:)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jan 17, 2007 11:31 am
by s.dot
Cannot modify header information - headers already sent by...

the rest of that line should tell you precisely where the error is. ;)

Posted: Wed Jan 17, 2007 11:39 am
by josefv
I am aware where the problem lies, but it's not obvious enough to me. Any further help for this numb brain of mine will be appreciated.

Posted: Wed Jan 17, 2007 11:44 am
by Luke
This has been answered at least a million times here and elsewhere...
http://www.google.com/search?q=Cannot+m ... nformation

shorteded link

Please search the forums before asking a question to see if it's already been answered. :)

Posted: Wed Jan 17, 2007 11:52 am
by josefv
Kindly, that's what I did. I've come across the "whitespace error", but the config.php file contains none. I've looked at various code samples, but would love to know specifically what is wrong with my snippet.

Thanks for all the advice in advance.

Posted: Wed Jan 17, 2007 11:55 am
by aaronhall
Post the line that the warning is pointing you to (it's either whitespace or another header() call)

Posted: Wed Jan 17, 2007 12:00 pm
by josefv
It's pointing my to a line #31 which is the following:

Code: Select all

header("Content-type: image/jpeg");
I've marked it above with "PROBLEM LIES HERE"

Posted: Wed Jan 17, 2007 12:03 pm
by nickvd
*sigh* :roll:

Posted: Wed Jan 17, 2007 12:05 pm
by aaronhall
Is that were the header was first sent, or is that the line at which the warning is being issued?

Posted: Wed Jan 17, 2007 12:08 pm
by josefv
Both!?!? 8O

It's pointing to itself, if that makes sense. It's the same file, on exactly the line number the header call appears, and it's also the first time I'm using header.

I haven't used the function elsewhere.

Posted: Wed Jan 17, 2007 12:13 pm
by aaronhall
Post the full error

Posted: Wed Jan 17, 2007 12:14 pm
by Luke
why don't you just read the results you got... I guarantee reading them would solve your problem.
viewtopic.php?t=59864&highlight=cannot+ ... nformation

Posted: Thu Jan 18, 2007 12:13 am
by josefv
Thanks for your help guys!

Ninja, the last couple of links you sent were most helpful! My problem is clearly the fact that I'm new to PHP, and secondly, data being output before my call to header. I shall try and encapsulate my code in a separate .php file (which might work the best as the code runs in a loop), or I might use output buffering.

Thanks guys!!!

:)

Posted: Thu Jan 18, 2007 12:23 am
by Luke
check you out! using terms like "encapsulate" and "output buffering"... you're already on your way to becoming a php guru josefv. :)

Posted: Thu Jan 18, 2007 1:12 am
by RobertGonzalez
Just remember all calls to session_start(), header() and setcookie() need to occur before any, yes any, output to the browser. This is a common error with a common solution... don't output anything before calling those function.