-3

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

Community
  • 1
  • 1
kapitanluffy
  • 1,269
  • 7
  • 26
  • 54
  • 3
    I am not sure what you are asking, can you clarify? – Pekka Aug 31 '11 at 16:23
  • i am asking if im the only one experiencing this. i tried it on my friend's pc. but it said headers already sent – kapitanluffy Aug 31 '11 at 16:28
  • Looks like no one got your question. I hope them read it again. – sidyll Aug 31 '11 at 16:29
  • If only this error message was explained somewhere - like in the manual, [`header`](http://php.net/header) – mario Aug 31 '11 at 16:30
  • i am actually expecting the headers already sent error here. but it 'miraculously' redirected – kapitanluffy Aug 31 '11 at 16:32
  • srsly i am disappointed that it got closed :| please read the post again thank you. and if you can open this thread again .it will be a relief if this confusion of mine gets answered :| – kapitanluffy Aug 31 '11 at 16:34
  • @kapitanluffy: The answers here are not for decoration. You are supposed to read them. You have output buffering enabled then. -- To avoid closed posts it's often very helpful to invest a bit more effort to make the question understandable instead of vague and misleading. – mario Aug 31 '11 at 16:34
  • yes i do read the answers. well i must say the post is "not for decoration" too. so you mean the 'Output Buffering' on the php.ini? now i remembered! thank you. everyone was like telling me to use ob_start .everyone is assuming that i do have the 'headers already sent' error. /face – kapitanluffy Aug 31 '11 at 16:41
  • @kapitanluffy: why do you **expect** any 'headers already sent' error at all, when **explicitely redirecting** by using `header('Location: url');`? You expect redirections NOT to work? – Jürgen Thelen Aug 31 '11 at 17:05
  • yes. because on my other pc it doesn't work which means there is something wrong with my code. that is why i am asking here :) – kapitanluffy Aug 31 '11 at 20:09

3 Answers3

2

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.

Layke
  • 51,422
  • 11
  • 85
  • 111
  • so you mean even i put the echo above the header. the header(Location..) gets sent first? – kapitanluffy Aug 31 '11 at 16:27
  • No, I mean that it **CANT** send that, because all of the headers have already been sent. There are lots of headers.. Content-Type etc.. things which the client needs in order to know what document type to expect. All of these headers must be sent before **anything** is outputted by the server. Even a single whitespace. – Layke Aug 31 '11 at 16:29
  • @Layke read the question again – sidyll Aug 31 '11 at 16:30
  • Sorry, that was not clear to me when you first asked the question... only after the edit is that clear. – Layke Aug 31 '11 at 16:37
1

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

Joe
  • 46,419
  • 33
  • 155
  • 245
0
<?php 
 ob_start();
 echo "header is trolling";
 ob_clean();
 header('Location: http://google.com');

?>
genesis
  • 50,477
  • 20
  • 96
  • 125