headers_sent()This function tells us about the status of headers. If output is posted to browser then we should not use header functions like redirect etc. To avoid such errors we can check status of headers by using headers_sent() function. It returns TRUE or FALSE based on the status of the header. When we execute a php script the outputs are stored in buffer before sending them to browser. However it also depends on your server php.ini setting where output buffer has to be on or off.
Now since output buffering is off so all output will be posted to browser. Let us learn by one example.
The output of above code will depend on your php.ini setting. For us our php.ini says like this .
In the above code in first line we posted output to browser but still we are getting that header_sent() is not giving output as buffer has not yet posted the output to browser. So we will get the output like this
So to send the output to browser we must exhaust the output buffer capacity. Here it is 4096. So we will try to send some more output to browser to exceed the limit and then check the if condition to know the header status. Let us learn from this example.
In the above code we have used ob_start() and ob_get_length() to know the data in buffer. You can adjust the for loop by increasing or decreasing the $i value. Higher number of loops mean more data will be posted to buffer. Try by increasing maximum value of $i and see where the headers_sent() became TRUE.
We can force to send the buffer output to browser by using flush(). However this depends on server and it may work differently.
This article is written by plus2net.com team.