Warning session_start () cannot send session cookie

Source: Internet
Author: User
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.ArticleHope someone can solve this problem perfectly!

A strange PHP problem caused by a UTF-8 BOM
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 XXX 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

CompileCodeThis error may occur even to the experts!

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. If so, it also identifies 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.

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

This problem also occurs if you include a. php,Generally, it is recommended that the files that are often included do not end with?>
Another example is that no output is allowed before session_start () is called. For example, the following is 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.

3. Add @ before session_start (), set_cookie (), and header () to suppress this warning.

4. In the editplus Editor, if the UTF-8. PHP file conversion to gb2312 or other, and then converted to UTF-8 so that you can successfully access, that is to say, the BOM at the beginning of the file is removed, this time the UTF-8 is no Bom type

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

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 append FF Fe, which can be clearly seen in the hexadecimal editor. In this way, when session_start () is called, two bytes are actually output to the browser, but the following warning is displayed in the invisible character 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 UTF-16 FF fe
UTF-32 big endian 00 00 Fe FF
Little endian UTF-32 FF Fe 00 00

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.