Home>Article>Backend Development> What to do if php header doesn’t work
Methods for php header not working: 1. Delete the space between location and ":"; 2. Delete the output before using the header; 3. The PHP code after the header is executed.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
What should I do if the php header does not work?
Cause analysis and solutions for PHP Header failure
When using header("location:test.php") to jump in PHP, please pay attention to the following A few 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 ones include setcookie() and session functions. These functions require adding 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 segments:
//There should be a blank line here
Reason:
Start of PHP script When executed, it can send http message header (title) information and body information at the same time. The http message header (from 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 (such as the Content-Type header). However, once the script sends any non-header output (for example, using HTML or a print() call), then PHP must first After sending all the headers, then terminate the HTTP header. Then continue to send the body data. From this point on, any attempt to add or modify the 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 use the cache function ob_start() in the program , 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.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php header doesn’t work. For more information, please follow other related articles on the PHP Chinese website!