PHP error Warning: Cannot modify header information - headers already sent by solution, warningheaders_PHP tutorial

WBOY
Release: 2016-07-13 10:18:19
Original
727 people have browsed it

PHP error Warning: Cannot modify header information - headers already sent by solution, warningheaders

I encountered this error today while testing the following code:

Copy code The code is as follows:

session_start();
$_SESSION['username']=$username;
echo "";
exit();

An error occurred:

Copy code The code is as follows:

Warning: Cannot modify header information - headers already sent by...

After looking at some online methods, I didn’t find a solution. Finally, I configured output_buffering in php.ini to default to 4096 and no longer encountered this error:

output_buffering setting instructions:

Off: Indicates turning off PHP output caching
On: Turn on infinite output buffer
4096: Open output cache with size 4096Byte

By default, php buffer is enabled, and the default value of the buffer is 4096, which is 4kb. You can find the output_buffering configuration in the php.ini configuration file. When echo, print, etc. output user data, the output data will be written to php output_buffering. Until output_buffering is full, the data will be sent to the browser through tcp. show. You can also manually activate the php output_buffering mechanism through ob_start(), so that even if the output exceeds 4kb of data, the data is not actually handed over to tcp and passed to the browser, because ob_start() sets the php buffer space to be large enough. The data will not be sent to the client browser until the end of the script or the ob_end_flush function is called.

For a detailed introduction to output_buffering, please refer to:

http://www.bkjia.com/article/55707.htm

Supplement: Of course you can also solve this problem by removing the BOM. It is recommended to encode in UTF-8 BOM-free format. Thanks @ihipop children’s shoes
Regarding the BOM, simply speaking, the software uses the BOM to identify whether the file is UTF-8 encoded. In early versions of Firefox, extensions could not have BOMs, but versions after Firefox 1.5 have begun to support BOMs. Now I discovered that PHP does not support BOM either. PHP did not consider the BOM issue when it was designed, which means that it will not ignore the three characters of the BOM at the beginning of the UTF-8 encoded file.

Another trouble was mentioned: "Limited by the COOKIE sending mechanism, in files that already have BOM at the beginning of these files, the COOKIE cannot be sent (because PHP has already sent the file header before the COOKIE is sent), so logging in and The logout function is invalid. All functions that rely on COOKIE and SESSION are invalid. "This should be the reason why a blank page appears in the WordPress background, because any executed file contains a BOM, and these three characters will be sent. Functions that rely on cookies and sessions are invalid, so you may also encounter the following errors:

Copy code The code is as follows:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at E:webindex.php:1) in E:webfunctionssessions.php on line 39

PHP page error: Warning: Cannot modify header information - headers already sent by

Open php.ini and set output_buffering to on. Restart appache, OK.

PHP test error, Warning: Cannot modify header information - headers already sent by

1. Sessionstart generally does not need to be called, and the server automatically handles it.
2. The two pieces of script code should be placed in the appropriate positions in or .
3. Your "header("location:login.php");" code should be placed in "

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/885650.htmlTechArticlePHP error Warning: Cannot modify header information - headers already sent by solution, warningheaders encountered while testing the following code today To this error: Copy the code as follows...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template