" in the include page! ! 3. The PHP code after the header will also be executed. Continued: Problem: Generally speaking, the input content before the header function does not appear before the header function."/> " in the include page! ! 3. The PHP code after the header will also be executed. Continued: Problem: Generally speaking, the input content before the header function does not appear before the header function.">
Home > Article > Backend Development > Analysis and solutions to the causes of PHP Header failure
When using header("location:test.php") to jump in PHP, please pay attention to the following points:
1. There cannot be a space between location and the ":" sign. , otherwise an error will occur.
2. There cannot be any output before using the header, and there must be no space after the tag "?>" in the include page! !
3. The PHP code after the header will also be executed.
Continued:
Problem: input content before the header function
Generally speaking, html content cannot be output before the header function , similar to setcookie() and session functions, these functions need to add message header information to the output stream. If there are statements such as echo before header() is executed, when header() is encountered later, a “Warning: Cannot modify header information – headers already sent by….” error will be reported. That is to say, there cannot be any text, blank lines, carriage returns, etc. in front of these functions, and it is best to add the exit() function after the header() function. For example, in the following incorrect writing, there is a blank line between the two PHP code snippets:
//There should be a blank line here
Reason:
When the PHP script starts executing, it can send the http message header (title) information and the body information at the same time. The http message header (from the header() or SetCookie() function) is not sent immediately. Instead, it is Saved to a list. This allows you to modify the header information, including the default header (e.g. Content-Type header). However, once the script sends any non-header output (e.g. using HTML or a print() call) , then PHP must first send all Headers, then terminate the HTTP header. Then continue to send the body data. From this point on, any attempt to add or modify Header information is not allowed, and one of the above error messages will be sent. .
Solution:
Modify php.ini to open the cache (output_buffering), change output_buffering=0 to output_buffering=4096
or in the program Use cache functions ob_start(), ob_end_flush(), etc. The principle is: when output_buffering is enabled, PHP does not send the HTTP header when the script sends output. Instead, it pipes this output into a dynamically growing cache (only available in PHP 4.0, which has a centralized output mechanism). You can still modify/add headers, or set cookies, since headers are not actually sent. When all scripts terminate, PHP will automatically send HTTP headers to the browser, and then send the contents of the output buffer.
The above is the analysis and solution of the causes of PHP Header failure. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!