Hi
I am getting this very error.....
Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\gallery2\examples\live-excel.php:36)
I have tried ob_start(); but of no use. It did not work....
can someone give the solution.??
Thanks in advance
cheers
Prati
Output buffering is not supported on all servers. (Though most do).
To test if you have Output buffering support try this on a php page on your server::
<?php
ob_start();
echo 'dummy content';
if (headers_sent() || !ob_get_length()) {
echo 'Your sever does not support output buffering';
} else {
echo 'Output buffering supported';
ob_clean();
}
?>
If your server does support output buffering, then it may be that the buffer is being dumped prematurely before you're sending your headers. This could be happening if you're using flush() or ob_flush() before you use header().
When you use the header() function, you should always check if headers have been sent or not...
Here's an example function to use in place of header():
function safe_header($str) {
if (!headers_sent()) {
header($str);
return true;
}
return false;
}
What way you get a return of false if the header could not be sent.
Example Use:
if (safe_header('Location: login.php')) {
die;
} else {
echo '<script>location = 'login.php';</script>';
echo 'Please <a href="login.php">Login</a>';
die;
}
Note that if headers are already sent then you cannot send any more headers or you'll get the error you're getting. So instead you can send some content such as JS to try and redirect the page. If the user does not use JS, then you'll have to fallback to a link.