Resolve "Cannot modify header information

Source: Internet
Author: User


Cannot have any output before sending header message

Any function that modifies or sends HTTP headers must be invoked before any form of output

Some functions modify HTTP headers:

Header/header_remove

session_start/session_regenerate_id

Setcookie/setrawcookie

And the main output may be as follows:

Implicit output
A space before or after the <?php or?>
BOM Header for UTF-8 file
Error messages that occurred before
Dominant output
Print, Echo, and other similar output functions
Raw HTML embedded between <?php
Why the Cannot modify header Information–headers already sent by error appears

To understand why the header message must be earlier than the output, let's look at the typical HTTP response. PHP primarily generates HTML content, but it also transmits a set of http/cgi headers to webserver:

http/1.1 OK

powered-by:php/5.3.7

Vary:donttrackmehere

content-type:text/html; Charset=utf-8

<body>

and <a href= "/" > </a>
The output is always a trailing header message. PHP must first send a header message to the Web server, and only one can be passed. You can no longer add any content to the header message after two lines are wrapped.

When PHP receives the first output (example: Print,echo,...), it refreshes and sends all header information for the setting. You can then output as you like, but you cannot modify the HTTP header information at this time.

How to find output older than a header message

The error message already contains all the information related to locating errors:

Warning:cannot Modify header Information-headers already sent by (output started at/www/usr2345/htdocs/auth.php:52) in /www/usr2345/htdocs/index.php on line 100
You can see that the header is invoked on the 100 lines of the file.

"Output started at" contains more important information: The 52 lines directly named in auth.php have produced output. That's what we need to find.

General situation:

Print,echo
The original HTML statement

<! DOCTYPE html>

<?php
The header message cannot be modified or sent.
Space before <?php
<?php
Here is a space
UTF-8 BOM

This is more difficult to detect but more prevalent. Be sure to save the file with your editor in "UTF-8 (no BOM)" mode.

The error message is shaped like "Unknown on line 0"

Generally, such an error indicates that the output is from the PHP extension or the php.ini set wrong message

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.