Home  >  Article  >  Backend Development  >  How to solve the problem of php header failure

How to solve the problem of php header failure

藏色散人
藏色散人Original
2021-10-20 11:12:535054browse

Solution to invalid php header: 1. Cancel the space between location and ":" symbol; 2. There should be no output before using header; 3. Modify php.ini to open cache and change "output_buffering=0" is changed to 409.

How to solve the problem of php header failure

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to solve the problem of php header failure?

Cause analysis and solutions for PHP Header failure

Use header("location:test.php") in PHP Please pay attention to the following points when making a jump:

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 header function

Generally speaking, html content cannot be output before the header function. Similarly, there are 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

Cause:

When the PHP script starts executing, it can send http message header (title) information and body information at the same time. The http message header (from header() or SetCookie() function) does not immediately sent, 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 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 will be sent One of the above error messages.

Solution:

Modify php.ini to open the cache (output_buffering), and change output_buffering=0 to output_buffering=4096

Or use the cache functions ob_start(), ob_end_flush(), etc. in the program. 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 How to solve the problem of php header failure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does php x86 mean?Next article:What does php x86 mean?