Cannot modify header information - headers already sent by

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maya

    #1

    Cannot modify header information - headers already sent by

    yes I know, this has been asked many times, but I don't quite the
    answers: usu. answers are to put the redirect code -- in my case

    $URL =
    "out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.";
    header("Locatio n: $URL");

    BEFORE any HTML output (does this include 'echo' output's?)

    but: if, like in my case, for example, I delete data (passed in req)
    from db THEN I redirect pg back to referer, how can I put redirect code
    BEFORE anything else? I have to do db stuff, disconnect from db, THEN
    do the redirect.. this is my code at then of file in question:

    <html><head>css .. etc.. </head><body>
    php: do db stuff... then:
    mysql_close($co nn);
    echo "<br><br><b>con nection to db closed.</b><br><br><br>\ n\n";
    $URL = "out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.";
    header("Locatio n: $URL");
    ?>
    </body>
    </html>

    (this is not a serious problem since can do with JavaScript, but just
    wondering what the deal is here... in Java you do redirect with RESPONSE
    object, and you can put redirect code wherever you want.. I don't quite
    understand why this is a problem in PHP..)
    (does $_RESPONSE exist in PHP? I looked here,
    http://us3.php.net/reserved.variables.. I see $_REQUEST, but not
    $_RESPONSE..

    thank you very much..




  • Colin McKinnon

    #2
    Re: Cannot modify header information - headers already sent by

    maya wrote:
    [color=blue]
    > yes I know, this has been asked many times, but I don't quite the
    > answers: usu. answers are to put the redirect code -- in my case
    >
    > $URL =
    > "out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.";
    > header("Locatio n: $URL");
    >
    > BEFORE any HTML output (does this include 'echo' output's?)
    >[/color]

    Yes - everything outside of <?php ... ?> and every print and echo inside.
    [color=blue]
    > but: if, like in my case, for example, I delete data (passed in req)
    > from db[/color]

    So how does that cause output to the browser?
    [color=blue]
    > THEN I redirect pg back to referer, how can I put redirect code
    > BEFORE anything else?[/color]

    Using a POST/GET redirection pattern is messy even when you know what you
    are doing. And if you knew what you were doing you wouldn't use
    header("Locatio n:...") for the redirection like that. It may have become
    standard practice for HTTP/1.0 but just plain wrong for HTTP/1.1. Have a
    google for discussion on the topic.
    [color=blue]
    > <html><head>css .. etc.. </head><body>
    > php: do db stuff... then:
    > mysql_close($co nn);
    > echo "<br><br><b>con nection to db closed.</b><br><br><br>\ n\n";
    > $URL =
    > "out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.";
    > header("Locatio n: $URL"); ?>
    > </body>
    > </html>
    >[/color]

    Try doing the php stuff before you do <html>. You can work around it by
    using output buffering but you're just digging a bigger hole for yourself
    if you do by making your code more complex and jumbled than it needs to be.
    [color=blue]
    > (this is not a serious problem since can do with JavaScript, but just
    > wondering what the deal is here... in Java you do redirect with RESPONSE
    > object,[/color]

    php != java
    php != asp
    php != perl
    php != ecmascript
    php != Visual BASIC

    If you want to write a framework of objects so it behaves similarly - then
    do so - its quite possible. You can even recreate those fun null reference
    errors (memory leaks, application server recycles, and horrendous build /
    deployment cycles are much harder to emulate though).

    Or a better idea might be to go find a templating system.

    C.

    Comment

    • Gordon Burditt

      #3
      Re: Cannot modify header information - headers already sent by

      >yes I know, this has been asked many times, but I don't quite the[color=blue]
      >answers: usu. answers are to put the redirect code -- in my case
      >
      > $URL =
      >"out.php?msg=s elected+records +have+been+dele ted+from+the+da tabase.";
      > header("Locatio n: $URL");
      >
      >BEFORE any HTML output (does this include 'echo' output's?)[/color]

      Absolutely, it includes 'echo' output. ANY OUTPUT AT ALL is a problem,
      including <html>, DOCTYPE, white space, warning messages, markers at
      the beginning of UTF files.

      You redirect or you present a page. You don't get to do both.
      And if you try, the page you try to present won't be seen.
      [color=blue]
      >but: if, like in my case, for example, I delete data (passed in req)
      >from db THEN I redirect pg back to referer, how can I put redirect code
      >BEFORE anything else? I have to do db stuff, disconnect from db, THEN
      >do the redirect.. this is my code at then of file in question:
      >
      ><html><head>cs s.. etc.. </head><body>[/color]
      ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^ THIS IS THE PROBLEM!!![color=blue]
      >php: do db stuff... then:
      >mysql_close($c onn);
      >echo "<br><br><b>con nection to db closed.</b><br><br><br>\ n\n";[/color]
      ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^ THIS IS ALSO A PROBLEM[color=blue]
      >$URL = "out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.";
      >header("Locati on: $URL");
      >?>
      ></body>
      ></html>
      >
      >(this is not a serious problem since can do with JavaScript, but just
      >wondering what the deal is here... in Java you do redirect with RESPONSE
      >object, and you can put redirect code wherever you want.. I don't quite
      >understand why this is a problem in PHP..)[/color]

      PHP runs on the *SERVER*.
      [color=blue]
      >(does $_RESPONSE exist in PHP? I looked here,[/color]

      No.

      Gordon L. Burditt

      Comment

      • maya

        #4
        Re: Cannot modify header information - headers already sent by

        Gordon Burditt wrote:
        [color=blue]
        > You redirect or you present a page. You don't get to do both.[/color]

        I guess you're right, no need for html output if all you're doing is
        db-stuff then redirecting..
        [color=blue]
        > And if you try, the page you try to present won't be seen.
        >
        >[color=green]
        >>but: if, like in my case, for example, I delete data (passed in req)[/color]
        >[color=green]
        >>from db THEN I redirect pg back to referer, how can I put redirect code[/color]
        >[color=green]
        >>BEFORE anything else? I have to do db stuff, disconnect from db, THEN
        >>do the redirect.. this is my code at then of file in question:
        >>
        >><html><head>c ss.. etc.. </head><body>[/color]
        >
        > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^ THIS IS THE PROBLEM!!!
        >[color=green]
        >>php: do db stuff... then:
        >>mysql_close($ conn);
        >>echo "<br><br><b>con nection to db closed.</b><br><br><br>\ n\n";[/color]
        >
        > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^ THIS IS ALSO A PROBLEM
        >[color=green]
        >>$URL = "out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.";
        >>header("Locat ion: $URL");
        >>?>
        >></body>
        >></html>
        >>
        >>(this is not a serious problem since can do with JavaScript, but just
        >>wondering what the deal is here... in Java you do redirect with RESPONSE
        >>object, and you can put redirect code wherever you want.. I don't quite
        >>understand why this is a problem in PHP..)[/color]
        >
        >
        > PHP runs on the *SERVER*.[/color]

        Of course I know PHP runs on the server.. what I meant is..
        <?
        all php..
        mysql_close($co nn);
        ?>
        <script language="JavaS cript" type="text/javascript">
        location =
        'out.php?msg=se lected+records+ have+been+delet ed+from+the+dat abase.';
        </script>



        [color=blue]
        >
        >[color=green]
        >>(does $_RESPONSE exist in PHP? I looked here,[/color]
        >
        >
        > No.
        >[/color]
        ok, good to know.. thank you...


        Comment

        Working...