PHP Error Warning:cannot Modify header information-headers already sent by workaround

Source: Internet
Author: User
Tags php error blank page

This article mainly introduces PHP error Warning:cannot modify header information-headers already sent by workaround, the need for friends can refer to the following

The error was encountered today when testing the following code:

Copy CodeThe code is as follows:
Session_Start ();
$_session[' username ']= $username;
echo "<script language= ' JavaScript ' >location.href= '. /admin.php ';</script> ';
Exit ();

Error occurred:

Copy CodeThe code is as follows:
Warning:cannot Modify header Information-headers already sent by ...

Look at some of the online method is not resolved, and finally in the php.ini configuration output_buffering default to 4096 did not encounter this error:

output_buffering Setup Instructions:

Off: Indicates that the PHP output cache is turned off
On: Open an infinitely large output cache
4096: Open output cache with size 4096Byte

By default, PHP buffer is turned on, and the default value of this buffer is 4096, which is 4kb. You can find the output_buffering configuration in the php.ini configuration file. The output data is written to the PHP output_buffering when the user data is Echo,print, until Output_ Buffering is full, this data will be transmitted via TCP to the browser display. You can also manually activate the PHP output_buffering mechanism through Ob_start (), so that even if the output exceeds 4KB data, it does not really give the data to TCP to the browser, because Ob_start () set the PHP buffer space to large enough. Data is sent to the client browser only until the script finishes, or when the Ob_end_flush function is called.

For more information about output_buffering, refer to:

Http://www.jb51.net/article/55707.htm

Add: Of course you can also solve this problem by removing the BOM, it is recommended to UTF-8 no BOM format encoding. Thank @ihipop Kids Shoes
In terms of BOM, the software is simply a BOM to identify if the file is UTF-8 encoded. In the early version of Firefox, there is no BOM in the extension, but the version after Firefox 1.5 has started to support the BOM. It is now also found that PHP does not support the BOM. PHP did not consider BOM at design time, that is to say, he will not ignore the UTF-8 encoded file at the beginning of the BOM three characters.

One of the other problems mentioned was that "the cookie is limited by the mechanism, and the cookie cannot be sent in a file with a BOM at the beginning of the file (because PHP has sent the file header before the cookie is sent out), so the login and logout functions fail." All the functions that rely on cookies and session implementations are not valid. "This should be the reason why the blank page in WordPress background, because any of the executed file contains the BOM, these three characters will be sent, resulting in the reliance on cookies and session function is invalid, so you may also encounter the following error:

Copy CodeThe code is as follows:
Warning:session_start () [Function.session-start]: Cannot send session cookie-headers already sent by (output started at e:\web\index.php:1) in E:\web\functions\sessions.php on line 39

PHP Error Warning:cannot Modify header information-headers already sent by workaround

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.