Guy Rutenberg

Keeping track of what I do

Trailing Whitespace Causes a Session to be Destroyed in CakePHP

with 6 comments

While working on a new project using CakePHP, I’ve came across a weird problem. In one of the controllers the the session always came out as invalid, as

$this->Session->valid()

always returned false. I tried debugging this weird stuff, and it looked like all the session variables are unset. Using

$this->Session->error()

to get the last error returned “Config doesn’t exist”. After further debugging “Config” turned out to be an internal array that is saved by CakePHP to the session and holds various internal data (some of it is used for validation like user-agent hash). I kept printing more and more debugging data as well as looking at CakePHP’s trac system.

I found an interesting bug (ticket #4217) and it looked very promising, as it almost fully described my problem. Unfortunately, the solution offered didn’t seem to work for me. But it inspired me to try and start the session manually using session_start() instead of using Cake’s startup and activate methods of the Session Component.

I found out that the session_id() returned empty string. Luckily calling session_start() directly from the controller, gave me a lead. The session seemed to work well but a nasty error about headers already been sent showed up.

I little more investigation lead up that I had a trailing newline after my closing PHP tag in that controller file. Deleting this trailing whitespace completely solved the problem. No need anymore to manually start the session. It’s pretty annoying that such a small thing like trailing new line can cause such seemingly unrelated problems in CakePHP’s session handling.

Maybe CakePHP should add a little debug notice when the session doesn’t start because header were already sent. This can be done by modifying the else statement in the __startSession() method in cake/lib/session.php (line 557 in version 1.2.0.6311). I wonder what the reason they had not to inform the developer when such event happens, as I don’t see why someone will deliberately try to start the session after sending the headers, I think it only happens by mistake (at least most of the time).

Share and Enjoy:
  • del.icio.us
  • StumbleUpon
  • Ma.gnolia
  • Digg
  • Facebook
  • Mixx
  • Google
  • Furl
  • Simpy

Written by Guy

March 8th, 2008 at 1:36 am

Posted in PHP

6 Responses to 'Trailing Whitespace Causes a Session to be Destroyed in CakePHP'

Subscribe to comments with RSS or TrackBack to 'Trailing Whitespace Causes a Session to be Destroyed in CakePHP'.

  1. Thanks a lot for this post, I had the exact same problem.

    Maybe you should look into filing a bug?
    Maybe it’s not a problem in 1.2, I’ll look into it.

    Omi Chowdhury

    26 Jun 08 at 14:23

  2. Hi Omi,
    IIRC I’ve used CakePHP 1.2. There is no need to file a bug, because it’s not a CakePHP bug. It is a result of the fact that php prints out everything that isn’t surrounded by the tags and it must send headers before he does so, and a PHP session can’t be created once the headers were sent. This is just one design flaw that we’ll have to live with.

    Guy

    26 Jun 08 at 14:33

  3. [...] bit of Google turned me to this page, so it was just trailing [...]

  4. Hey, thanks for taking the time to write about this problem. This would have driven me crazy for quite some time.

    Kai

    15 Dec 08 at 10:25

  5. Thank you so much. I was banging my head on this for so long and couldn’t find an answer. I appreciate it.

    Tim Banks

    17 Dec 08 at 04:58

  6. I have solved that problem in my project by only writing

    $this->Session->start();

    Before the read the session value for authentication. I suggest you also that u also try it.

    Chunara

    6 May 09 at 08:06

Leave a Reply