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

    your problem is that

    include("MM_GetImage.php");
    

    is only going to work if it's at the TOP. You can't send a header (Location: in this case) after output has begun. This has nothing to do with IE by the way, this is a warning from PHP. You'd get the same in Firefox.

    This is an aside but it might help you grasp the concept. if you have putty or a command line on your server, try doing this to see what your headers are:

    [root#] curl -i www.yoursite.com/yourpage.php

    note the headers that are output to the browser. You don't "see" these, instead the browser uses these to handle what the output means. These can't be sent once output begins (symbolized by a blank line)

      So it does not matter that it is in a separate file... I thought being in a separate file meant a whole new ballgame, so to speak 😉

      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.

      Thank you very much for the clarity shared. 🙂

        a included or required file is not a "whole new ball game. If fileX.php was this:

        <?php
        echo 'hello world';
        ?>

        and your page was like this:
        <html>
        <head>
        </head>
        <body>
        <?php require('fileX.php'); ?>
        </body>

        then that is functionally identical to doing this:

        <html>
        <head>
        </head>
        <body>
        <?php
        echo 'hello world';
        ?>
        </body>

        Included files ARE modularity if you plan them right 🙂

          While probably the best solution is to organize things to ensure that any functionality that sets a header will occur before any output is generated, if there are constraints that make that difficult, you can use output buffering -- as long as you start the buffering before any output is generated.

          <?php
          ob_start(); // nothing before this and the opening php tag
          ?>
          rest of page . . .
          

            Fantastic!!!

            Suddenly, the "impossible" appears 100% likely 🙂

            Thank you for sharing your time and expertise.

            Gratefully mentored,
            ExpertAlmost (Not really...but it sounds better than "CluelessAlready")

              Write a Reply...