PHP Error: Cannot send session cache limiter - headers already sent
Question by SmartUser | 2012-04-21 at 20:15
Recently, I got the following error in a PHP script, which I can not explain, unfortunately:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at index.php:1) in index.php on line 5
As far as I know, I have not changed the files involved, but suddenly my PHP script stopped working and ends with this error message. Can someone explain that to me?
Related Topics
Lazarus: Program without GUI - Many WSRegister Errors
Question | 6 Answers
Website Performance: Caching and Expires Header for Images, CSS and JavaScript
Tutorial | 0 Comments
Reload Images, CSS, JS and Web Pages despite Browser Cache
Tip | 2 Comments
PHP: Check if session already exists - Prevent error "A session had already been started"
Question | 3 Answers
PHP: Sending an E-Mail
Tutorial | 0 Comments
PHP: File Download Script
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
You can only start a session with the function session_start(), if no information has been sent up to then. That is, before the session_start(), it is not allowed to output characters with echo and there shall not appear any HTML or text outside the PHP code, not even a space. Your source file has to start immediately with your PHP code.
One possible error source is that somewhere a space or a line break has crept in, for example, if you start with the "<?php" not until the second row. Then, the line break is outputed prior to the PHP-code as "character" and session_start() no longer works.
Another problem, on which to get is difficult, is the encoding of the file. If your file has a UTF-8 encoding, it may be, that at the beginning of your file, a byte order mark (BOM) is included. This tells the executing program that there is a UTF-8 file, but the BOM is not displayed in an editor. Thus, the BOM is sent unnoticed, but interpreted as the first character of the file and the session_start() does not work anymore, but you do not know why.
The solution is simple: Because the BOM in UTF-8 files is optional, you have to remove it and everything is working again. You can remove the BOM from files, for example, with the help of the tool TextConverter, in which you can simply change the file encoding via "File > Only Change File Encoding". Here you can simply select "Unicode UTF-8" as encoding and uncheck the option "Byte Order Mark".
If you edit and save your files that have no BOM with the Windows Editor, it may be, that the Editor automatically adds a BOM to the files and then it no longer works. In programs like Dreamweaver, this is not the case and everything stays the same.
The background of why it is not working, is the following: As soon as the first sign of a page is sent, the header is sent. And as soon as the header is sent, no session can be started.
2012-04-24 at 16:50
Thank you! That was exactly the problem. I have removed the Byte Order Mark, and everything is working again.
I had really shortly seen the files in the Editor and saved them without changing anything. And with this, the BOM was suddenly there.. Well, so fast it can go..
2012-04-27 at 09:29