This article describes the PHP hints cannot modify header Information-headers already sent by solution, is the PHP design process often encountered problems. In this paper, an example is used to analyze the solution. Share to everyone for your reference. The specific methods are as follows:
Now take a look at this piece of code:
<?php
Ob_start ();
Setcookie ("username", "Test", Time () +3600);
echo "The username is:". $HTTP _cookie_vars["username"]. " \ n ";
echo "The username is:". $_cookie["username"]. " \ n ";
Print_r ($_cookie);
? >
When accessing the PHP file prompts Warning:cannot Modify header information-headers already sent by
Cause of the error:
The reason is that the head of the PHP program is added, header ("content-type:text/html; Charset=utf-8 "), and then the page appears with the above error.
Because header (' content-type:text/html;charset= UTF-8 '), no output before sending header, no space, you need to have header (...) Before the space is removed, or other output is removed, if he contains other files above, you have to check the other files have output.
On the Internet to check some information, said it is my php.ini inside the configuration out of the problem, find the php.ini file in the output_buffering default to OFF, change it to on or any number, but try to no results.
The Setcookie function must be sent out before any data is exported to the browser
Based on these limitations, the Setcookie () function is often encountered "Undefined index", "Cannot modify header Information-headers already sent by" ... And so on, the solution to the "cannot modify header Information-headers already sent by" method is to delay the data output to the browser before the cookie is generated, so you can add ob_ to the front of the program The start () function.
The Ob_start () function is used to open a buffer, such as the header () function, if there is output before, including carriage return \ space \ linefeed \ will have "Header had all ready send" error, you can first use Ob_start () The data Block and Echo () output of the open buffer PHP code will enter the buffer without immediate output!
The problem is resolved by the following methods:
Ob_start () before header ()
;///Open buffer
echo \ "hellon\";//Output
header ("location:index.php"); redirect browser to index.php
Ob_end_flush ()//output all content to browser
?>
I hope this article will help you with the learning of PHP programming.