Reason analysis and solving method of PHP header invalidation _php example

Source: Internet
Author: User
Tags php script setcookie

In PHP, use header ("location:test.php") to jump to note the following points:

1, location and ":" No space between the number, or there will be errors.

2, in the use of header can not have any output, including the page included in the label "?>" After no space!!

3, after the header of the PHP code will also be executed.

Continued:

Problem: Input before header function

In general, it is not possible to output HTML content before the header function, similar to the Setcookie () and session functions, which need to add message header information to the output stream. If there is an Echo statement before header () executes, when the header () is encountered, the "Warning:cannot Modify header Information–headers already sent by ..." error is reported. This means that there can be no text, blank lines, carriage returns, etc. before these functions, and it is best to add the exit () function after the header () function. For example, in the following error notation, there is a blank line between two PHP code snippets:

This is supposed to be a blank line.

Reason:

When the PHP script starts executing, it can simultaneously send the HTTP message header (header) information and the principal information. 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 headings (such as content-type headings). However, once the script sends any non-caption output (for example, using HTML or print () calls), then PHP must first send all headers and then terminate Httpheader. And then continue sending the principal data. From this point on, any attempt to add or modify header information is disallowed and sends one of the above error messages.

Workaround:

Modify PHP.ini to open cache (output_buffering) and modify output_buffering=0 to output_buffering=4096

Or use the caching function Ob_start (), Ob_end_flush (), etc. in the program. The principle is: When output_buffering is enabled, PHP does not send httpheader when the script sends output. Instead, it enters the output through the pipeline (pipe) into the dynamically incremented cache (which can only be used in PHP4.0, which has a centralized output mechanism). You can still modify/add headers, or set cookies, 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 contents of the output buffer.

Above this PHP header invalidation reason analysis and the solution is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.