Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Reasons why Symfony 2 sessions might not always persist

Have you spent countless hours trying to figure out why your session data isn't persisting from page to page? Or maybe you've been trying to call a page using AJAX / XMLHttpRequest, and then set a session; but once navigating to the next page, the session was gone. The big question here is - what causes this?

One of the first things to know, is that Symfony 2 overwrites the default session.save_path; and it has a different path for the development and production environments. If you're creating your sessions on the development environment, and then navigating to the production environment, it will not persist. You need to make sure you stay on the same environment the whole time, or it'll get lost in the mix.

Here are the paths where the session cache gets saved:


/app/cache/dev/sessions
/app/cache/prod/sessions

The next thing to check is your config.yml - make sure that your session lifetime is what you want. And if you're setting your sessions via an AJAX / XMLHttpRequest, make sure that you set cookie_httponly to false. Here's an example below:


session:         
        cookie_lifetime:         3600
        cookie_httponly:         false

Don't get me wrong - I think that Symfony 2 has a great wrapper for the native PHP session functionality, but it has it's oddities.

And for the majority of people, this might not even be an issue. Hope this helps.


jon | September 24, 2012 | Comments (0)

Comments


Name (required)
Email (will not be published) (required)
Website

captcha