开发者

PHP Headers already sending? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Warning: Cannot modify header information - headers already sent

have you encountered PHP's headers already sending even though you already showed content to the browser?

.Proof of开发者_StackOverflow Concept.

test.php

<?php 
 echo "header is trolling";
 header('Location: http://google.com');
?>

screenshot after loading test.php

http://i51.tinypic.com/2gvj6gi.png

it displays object not found at first (i am on a random page) i am trying to capture the 'Transferring data from Google' but it loaded so fast lol

if your gonna ask for my phpinfo just tell me what section to post :)

Just to CLARIFY i am not getting 'headers already sent' error i do know how to deal with that. i am actually expecting that error but it didn't showed up


Headers are sent immediately, as in before any actual document body is sent. By echoing, you are saying:

Hey, I'm finished adding all of my headers, lets move onto the document content! So start sending the headers to the user!

So then when you try and add a header, you can't because you have already sent them when you do

echo "header is trolling";

This is not some bug, it is perfectly normal. Think about it.. you have to send the headers first, because, what happens if you are gzipping your document? How can you possibly tell the client this, without first sending headers.


You should buffer the page content to prevent it from being sent before you send headers. An example on the doc page:

http://php.net/manual/en/function.ob-start.php


<?php 
 ob_start();
 echo "header is trolling";
 ob_clean();
 header('Location: http://google.com');

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜