Warning: cannot modify header information-headers already sent by (output started

Source: Internet
Author: User

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 .... "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:

<? PHP

// Some code here

?>

// It should be a blank line

<? PHP

Header ("HTTP/1.1 403 Forbidden ");

Exit ();

?>

The reason is: when the PHP script starts to be executed, it can simultaneously send the HTTP message header (title) information and 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), 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.

Warning: cannot modify header information-headers already sent by (output started

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.