WarningCannotsendsessioncookie-headersalreadysent... the solution of the problem (PHP UTF-8BOM introduced the habit of using editplus php programming, so sometimes there will be some unknown errors, very troublesome; recently, in the development project, some pages always encounter the following problems: Warning: session_start () [fu Warning Cannot send session cookie-headers already sent... solution to the problem
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!
I. A strange PHP problem caused by a UTF-8 BOM
// --- A. 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.
2. 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 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
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