CakePHP Session Troubleshooting

I use CakePHP on a day to day basis for project ranging from a single controller with a couple of views through to projects with large amounts of Controllers, Models, Views and Relationships.

This morning I was trying to build a simple Google Analytics component (which I will release as soon as it is done) and I needed to use the Session component…easy enough I hear you say…WRONG! I sat there for over an hour trying to figure out why my session was clearing every time I refreshed.

I searched high and low to find a solution, I tested the PHP session configuration by turning off “Session.start” in core.php and using the php “session_start()” function instead, everything worked fine.

After pulling out all my hair (what was left of it), I started thinking that it could be something to do with something being output before the header. I wasn’t getting any “Header Already Sent” warnings but I was still skeptical.

BINGO! I removed a single space after the closing php tag in one of my controllers and everything started working again.

It was so simple yet I have seem multiple threads on forums with the same problem but with no posted solution, hence why I am posting this for future reference (and when I inevitably forget).

So when troubleshooting CakePHP sessions make sure the first thing you do is remove any excess whitespace from your controllers, models and components.

A simple space after the ?> tag can cause all sorts of weird Session behaviour.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>