If you have been developing website that uses cakephp, you might stumble upon an annoying time when your session is not working. Here are some tips on how to make it work.
1) Configure::write(’Security.level’, ‘medium’);
On your core.php search for the line Security.level, by default this is configured to be high. Most of the time changing this will make your session work.
2) Configure::write(’Session.checkAgent’, false);
Also on your core.php, checkAgent will check your HTTP_USER_AGENT, which retrieves your browser and computer information, most of the time and i don’t know why IE will have a problem with this. By default the setting is set to true, try setting it to false and see if your session works.
3) session.cookie_path
Some times when your session.cookie_path is different from the root folder, Session and CakePhp will not work. A simple solution is to set Session.start on core.php to false. And on your AppController (app_controller.php) or on every Controller that you have to use session, set the following on the beforeFilter function
function beforeFilter(){
$this->Session->activate('/');
...
}
4) Headers already out before CakePhp gets the Session
This took me hours before figuring out what’s the problem with my Session and this is the reason why i wrote this post. Anyway, to check if your headers are out before cake fire session_start, first create a dummy page what will definitely fire a session, create it under webroot folder. For example create a file named session.php under webroot with the following code:
<?php
session_start();
$_SESSION['test'] = “testing”;
?>
Access it by your browser (http:/xxxx/session.php).
Next step is to go to your cake folder, then libs folder, then open session.php. Locate for the word “function __startSession” and change the following lines
function __startSession() {
if (headers_sent()) {
if (!isset($_SESSION)) {
var_dump($_SESSION);
$_SESSION = array();
}
return false;
.....
Now definitely there should be something on the $_SESSION, if not or you have an empty array then you have a problem with spaces on your files, you can check the controller your calling from, the models, the components and see if you have a space on the top or the bottom. If your sure you have none, then check if the file was meant to run under linux, windows or unix. If you still have a problem then check the file encoding, if you are using UTF-8 select the one without BOM. If you still have problem then ask google to help you.