Jump to content

Impossible (???) Headers Already Sent Error


ExpertAlmost

Recommended Posts

Using IE 7, I get a "Headers Already Sent Error" Even after having checked for all the usual suspects:

1) preceeding output/lines/blank spaces, no blank lines/spaces after my php block in an included file,

2) no BOM set (in Adobe Dreamweaver),

3) no virtual includes, session starts, cookies (header sending functions)

 

I have two stripped down simple test files: MM_Output2.php and MM_GetImage.php.

 

MM_Output2.php, some standard HTML and 3 php lines:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyTest</title>
<body>
<?php
include("MM_GetImage.php");
?>
</body>
</html>

 

And MM_GetImage.php, again 3 lines only!:

<?php
header("Location: MM_Output2.php");
?>

 

Any expert ideas why I would be getting a Header already sent message?  :wtf:

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\MM_Output2.php:7) in C:\AppServ\www\MM_GetImage.php on line 2

 

 

Interestingly, if I place the header redirect line BEFORE the PHP code block in the second file, my code works fine with NO header error. But oddly it then outputs the text: header("Location: MM_Output2.php"); to the top of the page! Why is that?  :o

 

In brief, I want to use my MM_Output2 file to display a graphic with Next/Previous Submit Form buttons below it. That calls MM_GetImage to determine the name/location of the new image to display and sends that variable back to MM_Output2. Basically allowing a user to scroll through a set of images.

 

I've spent hours playing with this and reading the same "things to look for..." on 15 different websites...and still no success. Please save this sinner from software purgatory  :-\

 

Thank you!!!

Link to comment
Share on other sites

Your outputting all this html before calling header:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyTest</title>
<body>

Link to comment
Share on other sites

Thank you Alex!!!

 

Yes of course I have all that html to output the first page. I broke up the programs functionality between two files so that I could show an image, do some processing, show a new image...so on and so forth.

 

All that header information is on the first page/file: MM_Output2. That page outputs a table with some images and my Next/Previous buttons. It then calls my second file, MM_GetImage, which calculates the next image to display based on either Next or Previous being submitted. Basically we just loops between those two pages.

 

Show_Image--Calc_Next_Image_On_Submit--Show_Image...repeat...

 

Perhaps I have a major conceptual disjunct?

 

How am I not seeing this correctly?

 

 

 

 

 

 

Link to comment
Share on other sites

Interestingly, if I place the header redirect line BEFORE the PHP code block...

 

You have got to be kidding. The header redirect statement header(....); is a line of php code. If you put it outside of the <?php ?> tags, it is no longer php code, it is a line of HTML content that is output to and rendered by the browser as some text on your page.

Link to comment
Share on other sites

Well...when it comes to being stupid...I always wish I was kidding  ::)

 

Thank you all for your gracious assistance. Suddenly, "impossible" seems obviously possible.

 

However, it seems I still have a serious conceptual disconnect.

 

Just how would I: Show an image, accept a submit click to determine a new image, show the new image...repeat? In other words: Show_Image-->On_Submit_Calc_Next_Image-->Show_Image-->repeat...

 

And I want to keep my code as modular as possible: the bulk of the php processing in a separate file from the HTML code.

 

Any conceptual assistance, web links to reference pages and/or code snippets would be very much appreciated.

 

And I will try to keep my kidding down to a minimum  ;)

 

Link to comment
Share on other sites

How does this look to you?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MyTest</title>
<body>
<img src='MM_Output2.php' />
</body>
</html>

 

Hope this clears it up a little.

Link to comment
Share on other sites

You certainly are confused.  There is no reason for you to be calling the header() function.  Just have your script emit the image with the next/prev buttons and have those be anchor tags that point back to the same script with url params, so what one would expect is something like:

 

MM_Output2.php?img=...&direct=prev

 

For the previous button, and direct=next for the next button.  Not knowing where the images come fron, it's hard to say what the img= param should be, but clearly it needs to indicate the current image.

 

At the top of your script you can look at the parameters using the $_GET[] superglobal array, and take whatever action is necesary.

 

Of course initially those params would be empty, so you need to have default values that setup the default image.

 

 

 

Link to comment
Share on other sites

Actually, I would consider it a great step forward to get to the "confused" level ;)

 

Thank you! Looks simple enough. But to a simpleton, everything looks simple! 5555

 

So I do NOT have to use a header redirect? The page, when complete, can point back to itself? And that is the loop? Looks like I missed something critical in my studies.

 

My php code determines file name and location of the next/previous image. So is this correct for the pseudo code in my HTML file:

 

1) php: Check $_post for any input.

2) php: If input, generate new filename, else use default name. (I can put 1 and 2 in a separate php file right?)

3) HTML: Show image as specified by the name variable.

4) HTML: Form submit buttons for next/previous.

5) ??? Go back to the top of the page when a button is clicked.

 

If that IS correct, the implementation of that last step is not clear. How do I get back to the top of my page when a button is clicked? Is there an HTLM code for that? Do I open another php block? Is there a property I do not understand?

 

WOW! I feel like we are getting so close...to an solution!  :D

I've written alot of PHP but no HTML (obviously).

 

You guys are great  8)

 

 

Link to comment
Share on other sites

The answer to your questions as I understand them, is yes.

 

The only thing I would clarify is that there is no "going to the top."

 

As I stated, your next/prev buttons have to either execute javascript, be regular anchor tags, or submit a form.  I don't know why things need to be complicated, but you mentioned checking the $_POST.  Regardless, the target should be the same script.

 

At this point the script isn't complicated enough to warrant seperate parts, although maybe a function or 2 wouldn't hurt much.  But if you really want to, you can have some of the code included.  It doesn't really matter, because the include() acts as if the included code were part of that script.  Since the inclusion itself seemed to be something that was causing confusion for you, I'd recommend just having all the code in one script initially, and you can break things out later when/if it makes sense.  Typically that occurs when you've established the need to reuse parts of the code, and at this point, you haven't established that from what I can see.

Link to comment
Share on other sites

Thank you everyone for sharing your time, experience and clarity!

 

I have a whole new coding direction to pursue tomorrow (I'm in Asia right now) and feel confident in making great strides--thanks to all of you  8) 

 

A large element of my confusion is I keep thinking in terms of "C/C++" when approaching HTML.

 

Experience is helpful, until it's not.

 

Gratefully,

ExpertAlmost (Not really, but it sounded a whole lot better than "IdiotAlready.")

 

 

Link to comment
Share on other sites

I keep thinking in terms of "C/C++" when approaching HTML.

 

I'm not sure, but I think you are missing how the client (browser) and web server (and any server side scripting language, like php) interact.

 

When you are 'viewing' a web page, the only thing you see and interact with in the browser is what the server has already sent to the browser. The web server and any php server-side script on a page has normally finished executing (unless your page takes a long time to generate on the server) by the time you see anything in the browser. You are not directly interacting with the web server and any php script you have on a page. The web server/php is not specifically sitting there waiting for a response from the browser due to a page that was just output. The web server has gone on to service other http requests and in fact it does not know or care about any http request before or after the current one. When you finally click on a link or submit a form in the browser, the browser makes a HTTP request to the web server and the web server does its' best to service that unique http request.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.