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.
Popularity: 1% [?]
You know the ?> at the end of PHP documents are not needed. I NEVER add them, and it simply solves the problem you just had.
Yeah it’s not required but I like to open and close all my tags…call me pedantic…lol
Thanks for letting everyone know that there is another way to ensure that CakePHP sessions work as desired.
Cheers,
Dean