Cannot modify header information - headers already sent by (output started a line error solve

 Why ?

Your issue doesn't appear to be in this snippet of code; this happens if you've sent anything to the user before calling the header() function. You need to make sure you don't send any data to the client (e.g. echoing something), as that will cause the headers to be sent, which means they can't be modified.



Solution

One of the easy way to prevent any output to disrupt the method header is to use buffering at the beginning of the page.


<?php
ob_start(); // Start buffering //

echo "Some echo";
header('Location: thiswillwork.php');
?>

The ob_start() function creates an output buffer. A callback function can be passed in to do processing on the contents of the buffer before it gets flushed from the buffer. Flags can be used to permit or restrict what the buffer is able to do.

Post a Comment

0 Comments