Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of php header failure

Shulou Source: shulou.com Published: 2022-06-03 03:13:52 10月14日 Update

The main content of this article is "how to solve the problem of php header failure". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the problem of php header failure.

The solution to php header failure: 1, cancel the space between location and ":" symbol; 2, do not have any output before using header; 3, modify php.ini to open the cache, and modify "output_buffering=0" to 409.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How to solve the problem of php header failure?

Cause Analysis and solution of PHP Header failure

Using header ("location:test.php") to jump in PHP should pay attention to the following points:

1. There must be no space between location and ":", or there will be an error.

2. You can't have any output before using header, including no spaces after the tag "? >" in the include page!

3. The PHP code after header will also be executed.

Continue:

Problem: input before header function

Generally speaking, you cannot output html content before the header function, similar to the setcookie () and session functions, which need to add header information to the output stream. If there are statements such as echo before the execution of header (), when header () is encountered later, it will report "Warning: Cannot modify header information-headers already sent by …." Mistake. That is to say, there cannot be any words, blank lines, carriage returns, etc., before these functions, and it is best to add the exit () function after the header () function. For example, the following error is written with a blank line between two php code snippets:

/ / this should be a blank line

Reason:

When the PHP script starts execution, it can send both the http message header (header) information and the body information. The http header (from the header () or SetCookie () function) is not sent immediately; instead, it is saved to a list. This allows you to modify the title information, including the default title (such as Content-Type title). However, once the script sends any output that is not a title (for example, using a HTML or print () call), PHP must first send all the Header and then terminate the HTTPheader. Then continue to send the subject data. From this point on, any attempt to add or modify Header information is not allowed, and one of the above error messages is sent.

Solution:

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

Or use cache functions such as ob_start (), ob_end_flush (), etc. The principle is that when output_buffering is enabled, PHP does not send HTTPheader when the script sends output. Instead, it pipe this output into the dynamically added cache (only available in PHP4.0, which has a centralized output mechanism). You can still modify / add header, or set cookie, because header is not actually sent. When all scripts are terminated, PHP automatically sends HTTP header to the browser, and then sends the contents of the output buffer.

At this point, I believe you have a deeper understanding of "how to solve the problem of php header failure". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Function output question information content title message cache script header method blank line error body between code reason actual learning input Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft Shulou Technology Shulou Information Redmi