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?
<?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();
}
?>
Remove the session_start() from inside the for loop.
Put the session_commit() outside the for loop at the very end.
Both these functions should only be called once in a script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With