I am having issues with using the session component in cakephp.
They worked fine throughout the site, when on a dev site on a different server.
After uploading the final version on the new server, it seems that the session will not start in certain controllers. Only on two controllers is this happening, but nothing seems to be done differently on these controllers - except possibly the fact that only these two controllers are using a custom file uploader component. Have tried disabling this and nothing changed.
The website is an ecommerce system, with the problems mainly occuring when trying to add an item to the basket, which is stored in a session. this session is remaining blank, whereas on the dev site worked a charm.
I am also using the auth component to login/logout, and although these 2 areas are not required to be logged in to function, they cause it to switch to logged out whenever they are loaded - and this then switches back by itself when any other areas of the site are accessed. The auth and session components are loaded in the appcontroller.
Thanks!
I've had a similar problem before now, and for me I was under the same impression as @starlocke that something needed kick starting. Instead of using Cake's Session component I used the session_start()
command, after checking that $_SESSION
was not defined. For example:
if(!isset($_SESSION)) session_start();
This was placed inside the beforeFilter() method of the AppController class.
I then also had a problem where it was saying it couldn't start the session because of there already being output to the server, this was solved for me by deleting any closing ?>
tags inside controllers, as these aren't required by php and stop any unneeded output being sent to the browser like in this case.
Further reading on php closing tags:
- Why I don’t use closing PHP tags
- PHP closing tag
My personal experiences have shown that CakePHP's sessions are independent of Apache+PHP sessions. With that in mind, it helps to take some action to always guarantee that a CakePHP Session (instead of the usual Apache+PHP session) is instantiated early in your website's workflow , for example, adding something like $this->Session->read('foo');
inside your site's AppController's beforeFilter()
. It shouldn't matter what key you try to read, and 'foo' should be enough to start your CakePHP sessions.