Cannot modify header information

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

josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Cannot modify header information

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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

the rest of that line should tell you precisely where the error is. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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. :)
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Post the line that the warning is pointing you to (it's either whitespace or another header() call)
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post 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"
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

*sigh* :roll:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Is that were the header was first sent, or is that the line at which the warning is being issued?
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Post the full error
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
josefv
Forum Newbie
Posts: 23
Joined: Wed Jan 17, 2007 11:17 am

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

:)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

check you out! using terms like "encapsulate" and "output buffering"... you're already on your way to becoming a php guru josefv. :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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