3 Solution for the error “Cannot modify header information”
SOLUTION 1 :
You can use output buffering to get around this problem.
ob_start();
header(’Location: welcome.php’);
ob_end_flush();
exit;
?>
This will save the output buffer on server and not output to browser yet, which means you can modify the header all you want until the ob_end_flush() statement.
Note the exit; statement after the header statement. Sometimes adding an exit; statement fixes some people’s problems.
SOLUTION 2:
Use Javascript instead of PHP (will not work in javascript-disabled browsers).
Since it’s a script, it won’t modify the header until execution of Javascript.
echo “<meta http-equiv=’Refresh’ content=’0; url=welcome.php’>”;
exit;
But don’t forget to add an exit; statement after the javascript because even though it is set to redirect
in 0 seconds, I found that it can still sometimes execute the php code below the script before redirecting (you don’t want that happening!). The exit; statement stops any php code below from executing.
SOLUTION 3:
Another popular Javascript for the same purpose (but it did not work for me!)
echo “<script> self.location(\”welcome.php\”);</script>”;
exit;
