Cannot send session cache limiter

Published on and tagged with cakephp  faq  problem

An often asked (PHP) question in the CakePHP IRC channel is how to fix the problem which causes the following warning:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at
/home/dho/projects/test/trunk/app/controllers/users_controller.php:7) in /home/dho/projects/cake_1.2.x.x/cake/libs/session.php
on line 131

It is a bit nasty. The warning points us to line 7 (the last line). Hm, but line 7 looks correct, doesn’t it?

<?php
    class UsersController extends AppController {
        var $uses = null;

        function edit() {}
    }
?> 

Well, if you mark line 7 with your mouse, you see that there is a space after the “>” character. And this space is the cause of the warning. If you remove the space, the warning disappears.

50 comments baked

  • autom

    This happened to me just yesterday!

  • KesheR

    tricky!

  • Boris Barroso

    Thanx for the warning!

  • Tarique Sani

    Another solution is not to use the php end tag ever….

  • cakebaker

    @Tarique Sani: Yes, that is another solution. But personally I don’t like it as it looks like something is missing ;-)

  • Tarique Sani

    @cakebaker: I perfer being meticulous as well but something miising is better than something amiss ;)

  • Dieter@be

    the best is just clean code.
    this is one of the most common problems for newbies (and very oftenly occuring situations for any php-dev in general)
    but even the best solution is extremely simple, so i really don’t see why anyone would do such things as omitting php closing tags (except for being a crappy programmer)

    Dieter

  • Tarique Sani

    @Dieter: Nowhere is it stated that closing tag is a must have in scripts which have just PHP code, thus calling the practice omitting the closing tags as crappy programming is just a personal opinion rather than a stated best solution or best practice. This is similar to why you cannot see anyone would do such a thing – it is your limitation…

    P.S. Are trying to flame me ;)

  • tork

    I have this problem but cannot fix it!

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at C:\……\index.php:9) in C:\…….\index.php on line 28

    but on line 9 i have:

    there are no extra spaces!!! i have checked the very last line of code and very first. there was blank spaces, but i got rid of those, still no result. what is the matter? I had this error twice, but once disappeared when I moved my “include” code before session_start(). any ideas?

  • tork

    Fixed the error!!! I did not have session_start() at the top of my code. I had it in the HTML area after . i hope this helps someone!!! :)

  • cakebaker

    @tork: I am glad you could solve your problem.

  • jdub

    Thanks!

    Wasted 2 or 3 hours of programming thanks to two little spaces. Won’t happen again though :)

    jdub

  • cakebaker

    @jdub: I think this is an experience every PHP developer has to make once in his life ;-)

  • carsten

    I did it…too! :D

    cheers & thx a lot
    carsten

  • Matt Kaufman

    thanks, weird. happened to me in a few of my models during a clean server move.

    to be serious, I almost expected it was something weird like this – and was really surprised when I tabbed back to this window and saw it was what I had expected, but not seriously enough to even consider looking at myself. So .. nice, thank you :)

  • SJ

    Thanks so much!! That error was almost driving me crazy! wasted a whole day over it… Thanks a million! :-)

  • cakebaker

    @SJ: I am glad this article helped you :)

  • hosein khoshraftar

    thanks very much. you help me very good
    be alive

  • cakebaker

    @hosein: I am happy to hear that :)

  • kynguyen

    Thank you so much. This bug drove me crazy for 2 days.

  • cakebaker

    kynguyen: Yeah, it is nasty. Cool to hear this post helped you :)

  • Yahesel

    This just happened to me!
    Thank you for posting this…
    It has totally reduced my stress level!
    I now love this site!
    :D

  • cakebaker

    @Yahesel: Thank you :)

  • dsa555

    awesome thnx!

  • sakhunzai

    great help

  • prytnar

    3h wasted
    thx for this post

  • Tharindu

    WoW !, I Almost fight with this problem from 8.00 Am to 1.18PM And Now Able to solve it Thanks to you’r Site…I’m s Sinhalese and I Wish you in my language bro!

    Budu Saranai Machan,
    JAYAWEWA !!!

  • cakebaker

    @Tharindu: Thanks, I am glad this article helped you :)

  • Quito

    Check the encoding: I accidentally saved the file as Unicode; I could not get rid off the following errror:

    Warning: session_start() [function.session-start]: Cannot send session cache limiter error…

    I tired searching for blank spaces, returns.

    Then I changed the encoding type to latin-1; and finally it worked.

  • cakebaker

    @Quito: Thanks for your hint that this error could also be caused by an encoding issue.

  • Aqib Mushtaq

    THANKS!!! I was trying to solve the same problem for about half an hour.

  • Natalie Chaykina

    thank you so much!!! you save me days of meanless work over that tricky problem! God bless you!

  • cakebaker

    @Natalie: You are welcome!

  • taymaz

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /home
    i can’t fix it.

  • cakebaker

    @taymaz: I hope you could fix your issue in the meantime ;-) If that’s not the case, can you paste the respective code to http://bin.cakephp.org?

  • shhadev

    Hey, i have a solution for that just start session in the top of the index page. And other solution is @

  • cakebaker

    @shhadev: Hm, I’m not sure whether starting the session at the top of index page really solves this issue. However, right now I’m no longer able to reproduce the issue itself…

  • spawn

    The problem can be from the corrupted characters before <?php . Those appears when you changed encoding ANSII to UTF-8. Edit your configuration.php with a HEX editor or re-encode it with Notepad++ and select "Encode in UTF-8 without BOM"

  • cakebaker

    @spawn: Thanks for your addition!

  • fagyan

    thanks a lot…

  • cakebaker

    @fagyan: You are welcome :)

  • sherif

    ohhh this happen with me in help page
    iam deleting this order
    /*

    */
    and all thing is very good
    but……………………
    it’s just in text area dont need login name or cookies or………
    what is better solution PLZ
    thanks alot
    sherif

  • cakebaker

    @sherif: I’m sorry but I don’t understand your question and what you try to accomplish… Can you describe a bit more what you are trying to accomplish? Thanks.

  • nayan
    <?php
    session_start();
    if (!isset($_SESSION['loginid']))
    {
    	echo "<script>location.replace('index.php');</script>";
    }
    ?>
  • cakebaker

    @nayan: Hm, what do you want to say with this snippet?

  • guillaume

    Worked for me.
    Except the spaces were Before the <?php tag.
    Maybe you should update your post with this bit of information.

    Thank you for the post !

  • cakebaker

    @guillaume: You are welcome!

  • Steve

    I am not programmer. Just trying to make my own website. I’m getting the following 3 error messages. After the error messages, I’ve posted my coding. The coding is a login page (login.php).

    The problem lies with the “start_session”. I’ve read on many blogs to get rid of the spaces etc. however, I do not want to start_session before the user has been authenticated (ie. username and passwords match).

    I’ve also read about the UTF8 (without Bom) which I’ve converted to using Notepad++

    Help!!

    Connected successfully”
    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at /hermes/web02/b1486/moo.aslsolutionsca/MyWebsite/db.php:11) in /hermes/web02/b1486/moo.aslsolutionsca/MyWebsite/login.php on line 23

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /hermes/web02/b1486/moo.aslsolutionsca/MyWebsite/db.php:11) in /hermes/web02/b1486/moo.aslsolutionsca/MyWebsite/login.php on line 23

    Warning: Cannot modify header information – headers already sent by (output started at /hermes/web02/b1486/moo.aslsolutionsca/MyWebsite/db.php:11) in /hermes/web02/b1486/moo.aslsolutionsca/MyWebsite/login.php on line 31

    <?php
    // Connect to the database
    require('db.php');

    // Set username and password variables for this script
    $user = mysql_real_escape_string($_POST["username"]);
    $pass = mysql_real_escape_string($_POST["password"]);

    // Make sure the username and password match, selecting all the client's
    // data from the database if it does. Store the data into $clientdata
    $clientdata = mysql_query("SELECT * FROM Clients WHERE username='$user' and password='$pass'")
    or die (mysql_error());

    // Put the $clientdata query into an array we can work with
    $data = mysql_fetch_array($clientdata, MYSQL_ASSOC);

    // If the username and password matched, we should have one entry in our
    // $clientdata array. If not, we should have 0. So, we can use a simple
    // if/else statement
    if(mysql_num_rows($clientdata) == 1){
    // Start a new blank session. This will assign the user's server
    // with a session with an idividual ID
    session_start();
    // With our session started, we can assign variables for a logged
    // in user to use until they log out.
    $_SESSION['username'] = $user;
    $_SESSION['email'] = $data['email'];
    $_SESSION['paypal'] = $data['paypal'];

    // Then, redirect them to the profile page
    header('Location: profile.php');
    }else{echo "The username and password don't match. Please go back and try again.
    (Or you could redirect them to the login page again.)";}

  • cakebaker

    @Steve: Hm, does it work if you put session_start() at the top of your file?

  • Manmohit Verma

    Yeah i had done this .
    Thanx you save my time over that tricky problem!

    There were a small space in code and i suggess all other coders to check that your session_start(0 would be on top of page

    Thanx

    :_)

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License