Reputation: 341
From what I have studied, headers cannot be changed after there is an output from a script. Why is it then that in this code sample that I wrote, the script has several outputs before it creates a new header, and yet everything works fine?
<?php
$name = "tommy" ;
?>
<?php
headers_sent($filename, $linenum) ;
header("name: $name") ;
echo "tommy <br>" ;
echo "Headers sent in $filename on line $linenum" ;
?>
In addition the output says that the headers were sent in line 0... how is that possible if I added a header afterwards?
Upvotes: 5
Views: 883
Reputation: 272106
If I understood your question correctly then I ask you to look at the value of output_buffering
directive in PHP.ini. The default value is 0 but the PHP installers often set it to 4096 (copying from php.ini-development or php.ini-production). This should be the case in your example i.e. the buffering is On.
Secondly, your code example has a flaw; it does not check the value returned by the headers_sent
function:
bool headers_sent ([ string &$file [, int &$line ]] )
Check the returned value first; consider the file and line parameters valid if the returned value is true:
<?php
$name = "tommy";
?>
<?php
$sent = headers_sent($filename, $linenum);
header("name: $name");
echo "tommy <br>";
if ($sent) {
echo "Headers sent in $filename on line $linenum";
} else {
echo "Headers not sent";
}
?>
Output:
tommy
Headers not sent
In summary:
Note: if the output_buffering is set to 4096 then this works:
<?php
echo str_repeat(" ", 4095);
header("X-Foo: Bar");
echo ".";
But this fails with headers already sent error:
<?php
echo str_repeat(" ", 4096);
header("X-Foo: Bar");
echo ".";
Upvotes: 1
Reputation: 36
Make sure you have no blank symbols before sending header in your case
<?php
$name = "tommy" ;
?>
<?php
headers_sent($filename, $linenum) ;
header("name: $name") ;
echo "tommy <br>" ;
echo "Headers sent in $filename on line $linenum" ;
?>
between ?>
and <?php
you have 5 new line characters which may cause the problem, if you include this file into another make sure main file has no any characters output above your header commands.
Upvotes: -1
Reputation: 830
Check if you have Output Buffering set to On in your php.ini. It might also be On by default on some PHP versions, according to this comment on php.net: http://www.php.net/manual/en/ref.outcontrol.php#69059
If that's the case, that's probably why your script executes fine with no errors, even though there is "output" before headers are sent.
EDIT: If you don't have access to the php.ini file itself, you can probably check the value of output buffering from your PHP script by calling ini_get('output_buffering');
.
Upvotes: 4
Reputation: 422
// -----------------------
ob_start();
// -----------------------
/* Code here */
// -----------------------
$output = ob_get_clean();
if(ob_get_level() > 0)
ob_end_clean();
echo $output;
// -----------------------
Upvotes: 0
Reputation: 16103
Output means, in this case, sending information to the client. Could be an echo
, print_r
or another form of outputting info in the screen.
There are more ways of outputting data, for example: setting a cookie will also send output. You can't set a cookie and proceed with header changes.
The error in your code:
On line 3 you close php ?>
and open it on line 5 <?php
. Line 4 is output, a \n
(newline-character). This will throw an error.
If you do not see that error, your error levels are probaly wrong. You can do an echo above your header, if you still get no errors, it's error_reporting.
Upvotes: 2