Cause Analysis and Solution of PHP Header failure, header cause analysis

Source: Internet
Author: User

Cause Analysis and Solution of PHP Header failure, header cause analysis

When using header ("location: test. PHP") in php to redirect, pay attention to the following points:

1. There must be no space between location and:. Otherwise, an error will occur.

2. There cannot be any output before using the header, including the "?>" tag on the include page" No space is allowed !!

3. the PHP code after the header is executed.

Continued:

Question: The content entered before the header Function

Generally, html content cannot be output before the header function. For example, there are setcookie () and session functions. These functions need to add message header information to the output stream. If an echo statement exists before the header () is executed, When header () is encountered later, "Warning: Cannot modify header information-headers already sent by…" is reported ...." Error. That is to say, there cannot be any text, blank lines, and carriage return before these functions, and it is best to add the exit () function after the header () function. For example, the following error code contains an empty line between two php code segments:

// It should be a blank line

Cause:

When the PHP script starts execution, it can send both the http message header (title) information and the subject information. 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 title information, including the default title (for example, Content-Type title ). however, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate HTTPheader. then send the subject data. at this time, 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) and change output_buffering = 0 to output_buffering = 4096.

Or use the cache functions ob_start () and ob_end_flush () in the program. Principle: When output_buffering is enabled, PHP does not send HTTPheader when the script sends the output. Instead, it inputs the output to the dynamically added cache through the pipeline (pipe) (which can only be used in PHP4.0 and has a centralized output mechanism ). You can still modify/Add the header or set the cookie because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the content in the output buffer.

The above PHP Header Failure Cause Analysis and Solution is all the content shared by the editor, I hope to give you a reference, and hope you can support the house of friends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.