Possible Duplicate:
Headers already sent by PHP
Below is a simple example of my PHP code which (I hope so) is self explanatory. What I try to do is to update the session variable. But the output of the script is as follows:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Library/WebServer/Documents/facebook/test.php:8) in /Library/WebServer/Documents/facebook/test.php on line 11
The warning is caused by the echo
statements in line 8 and 9, of course. Is there any simple solution to stop this warning.
Thanks for any pointers, Andrej
<?php
session_start();
$_SESSION['percent'] = 0;
$iterations = 50;
for ($i = 0; $i <= iterations; $i++) {
$percent = ($i / $iterations) * 100;
echo "Hello World!";
echo "<br />";
// update session variable
session_start();
$_SESSION['percent'] = number_format($percent, 0, '', '');
session_commit();
}
?>
The only solution that works (i.e. updates the session variable) for me is:
<?php
ob_start();
session_start();
$_SESSION['percent'] = 0;
$iterations = 50;
for ($i = 0; $i <= 50; $i++) {
$percent = ($i / $iterations) * 100;
echo "Hello World!";
echo "<br />";
// update session variable
session_start();
$_SESSION['percent'] = number_format($percent, 0, '', '');
session_commit();
}
ob_flush();
?>
It's ugly, while it buffers the output first...