Connecting Tech Pros Worldwide Forums | Help | Site Map

Cannot modify header information - headers already sent by

maya
Guest
 
Posts: n/a
#1: Jun 22 '06
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=selected+records+have+been+deleted+fr om+the+database.";
header("Location: $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($conn);
echo "<br><br><b>connection to db closed.</b><br><br><br>\n\n";
$URL = "out.php?msg=selected+records+have+been+deleted+fr om+the+database.";
header("Location: $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
Guest
 
Posts: n/a
#2: Jun 22 '06

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=selected+records+have+been+deleted+fr om+the+database.";
> header("Location: $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("Location:...") 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($conn);
> echo "<br><br><b>connection to db closed.</b><br><br><br>\n\n";
> $URL =
> "out.php?msg=selected+records+have+been+deleted+fr om+the+database.";
> header("Location: $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.
Gordon Burditt
Guest
 
Posts: n/a
#3: Jun 23 '06

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=selected+records+have+been+deleted+f rom+the+database.";
> header("Location: $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>css.. etc.. </head><body>[/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ THIS IS THE PROBLEM!!![color=blue]
>php: do db stuff... then:
>mysql_close($conn);
>echo "<br><br><b>connection to db closed.</b><br><br><br>\n\n";[/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ THIS IS ALSO A PROBLEM[color=blue]
>$URL = "out.php?msg=selected+records+have+been+deleted+fr om+the+database.";
>header("Location: $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
maya
Guest
 
Posts: n/a
#4: Jun 23 '06

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>css.. etc.. </head><body>[/color]
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ THIS IS THE PROBLEM!!!
>[color=green]
>>php: do db stuff... then:
>>mysql_close($conn);
>>echo "<br><br><b>connection to db closed.</b><br><br><br>\n\n";[/color]
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ THIS IS ALSO A PROBLEM
>[color=green]
>>$URL = "out.php?msg=selected+records+have+been+deleted+fr om+the+database.";
>>header("Location: $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($conn);
?>
<script language="JavaScript" type="text/javascript">
location =
'out.php?msg=selected+records+have+been+deleted+fr om+the+database.';
</script>



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


Closed Thread