Warning: cannot send session cookie-headers already sent

Source: Internet
Author: User
Tags php session

Warning cannot send session cookie-headers already sent... Problem Solving (problem caused by UTF-8 BOM of PHP)

I am used to using edit plus for PHP programming, so sometimes some unknown errors may occur, which is very troublesome;
Recently, the following problems always occur on some pages during project development:

Warning: session_start () [function. session-start]: cannot send session cookie-headers already sent by (output started at E:/web/apache2/htdocs/index. PHP: 1) in E:/web/apache2/htdocs/functions/sessions. PHP on line 67

Warning: session_start () [function. session-start]: cannot send session cache limiter-headers already sent (output started at E:/web/apache2/htdocs/index. PHP: 1) in E:/web/apache2/htdocs/functions/sessions. PHP on line 67
After detailed search, the following causes are obtained:
In my edit plus settings, the default encoding is UTF-8, And the utf_8 signature is: always add a signature;
Therefore, try the following operations:
In the edit plus tool> parameter> File> utf_8 signature, change the option "always add signature" to "always remove signature" and open index. PHP file, save it as a new one, and re-run the script, and finally it will work properly;

In addition, I found two useful articles on the Internet. I hope someone can solve this problem perfectly!

A UTF-8 BOM caused by PHP strange problem I,

// --- A. php
<? PHP
Header ("Content-Type: image/BMP ");
Session_start ();
................
?>
Save a. php as UTF-8. the following error occurs when you access the PHP file in a browser:
Warning: session_start () [function. Session-start]: cannot send session cache limiter-headers already sent (output started

At ×××. php: 1) In ××× on line 2

This problem is common, most of which are output before session_start! For laruence, this error basically does not happen, but if you use DW or editplus

This error may occur even if you are a programmer writing code!

As shown in the preceding figure, there are no output statements in line 2 of the XXX file and line 2 of the XXX file, it's strange. Why?
Originally:

Unicode signature (BOM) can include the byte sequence mark (BOM) in the document ). Bom is two to four bytes at the beginning of a text file. It can be identified as Unicode.

The byte sequence of the subsequent bytes. Because the UTF-8 does not have a byte order, you can choose to add a UTF-8 Bom. This is required for UTF-16 and UTF-32.
No! If this option is selected, 2 to 4 bytes will be output at the beginning of the page!

Session_start () requires no output to the client browser before.

II,

Another error may occur, for example:
/-- A. php --
?>
Empty row
Empty row

This problem also occurs when you include a. php files. We recommend that you do not include the files that are often included at the end?>

Another example:
No output is allowed before session_start () is called. For example, the following is an error.
Incorrect.
========================================================== =
1 line
2 rows <? PHP
3 rows session_start (); // output already exists in the first row
4 rows .....
5 rows?>
========================================================== =

It is indeed so strange that it has been tested.
III,

Session_start ()
Set_cookie ()
Header ()
The preceding @ should be used to suppress this warning.

IV,

In the editplus Editor, If you first convert the. php file of UTF-8 to gb2312 or another file, and then convert it to UTF-8, the file can be accessed successfully, that is

Bom is removed, at this time the UTF-8 is no Bom type

Php-session_start () error caused by UTF-8 encoding

14:55:01

Large, medium, and small
The default gb2312 encoding is compatible with ANSI encoding. the file header does not have any additional information. In this case, session_start () can work properly.
When UTF Encoding is used, most editors will append a BOM block to the file header. My editplus appends FF Fe and uses a hexadecimal editor.
You can see clearly. In this way, when session_start () is called, two bytes are actually output to the browser, except that the hidden characters are
The following warning is displayed in the browser:
Warning: session_start () [function. session-start]: cannot send session cookie-headers already sent by (output started ......................

Solution:
1. Manually remove the BOM block. You can edit it in a hexadecimal Editor, such as ultraedit, or use the built-in functions of the editor. A good editor generally provides the option of removing the BOM block.
2. Write a script to correct it. For different editors, the BOM header is defined as follows:
UTF-8 EF BB BF
UTF-16 big endian Fe FF
Little endian FF Fe UTF-16
UTF-32 big endian 00 00 Fe FF
UTF-32 little endian FF Fe 00

Solution

During the PHP session operation, if the following error occurs: Warning: cannot send session cookie-headers already sent... "or" cannot add header information-headers already sent ..."

You only need to set output_buffering in PHP. ini. The default value is no. You can change this value to 1.

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.