This is CakePHP 1.3
I have a 5-step order form that stores the new session data after each step. This works perfectly every step, until the last step where the user completes the order.
On the last step, I save the order, then set a session flag to say it’s been completed. This way, if the user refreshes the page it won’t put through another order again.
The problem is, I set the flag in the session and if I debug the session it’s there. On page refresh, the session has reverted completely to how it was before, without the new values.
// Controller action for step 5 of the form
public function step5() {
// FIRST DEBUG
debug($this->Session->read('order'));
// Load from session so that the view can display the order information
$this->data = $this->Session->read('order');
// Save order if not already completed
if (!$this->Session->check('order.complete')) {
// Adds to the database
if ($this->_saveOrder($this->data)) {
// Set flag so if the user refreshes, it won't save again
$this->Session->write('order.complete', true);
// SECOND DEBUG
debug($this->Session->read('order'));
}
}
}
In the first debug when the page loads, the session looks like this:
Array
(
[Order] => Array
(
[value1] => 4566
[value2] => 'test'
[value3] => 0
[value4] => 0
)
[Customer] => Array
(
[fname] => test
[sname] => test
[email] => test@torg.co.uk
[tel] => 0123456789
)
)
Then the second debug shows that the new session flag has been written to the session:
Array
(
[Order] => Array
(
[value1] => 4566
[value2] => 'test'
[value3] => 0
[value4] => 0
)
[Customer] => Array
(
[fname] => test
[sname] => test
[email] => test@torg.co.uk
[tel] => 0123456789
)
['complete'] => true
)
But then if I refresh the page, the complete flag is gone, the session has reverted entirely back to how it was before, and the save runs again because the flag isn’t there. There is NO code after the flag is written so it isn’t being deleted.
Array
(
[Order] => Array
(
[value1] => 4566
[value2] => 'test'
[value3] => 0
[value4] => 0
)
[Customer] => Array
(
[fname] => test
[sname] => test
[email] => test@torg.co.uk
[tel] => 0123456789
)
)
The session writing works fine on all other steps. I can add/update session variables between pages and they work fine and persist across pages and page refreshes.
I don’t know if this is something to do with my config settings or what. I don’t know why this is happening just on this page. I also noticed sometimes, very rarely, after tearing my hair out for AGES and clearing the cache a few times, etc. it will seem to briefly work once, then stop working again.
Could it be anything to do with my config settings? Here is my Config/core.php without the comments:
Configure::write('debug', 2);
Configure::write('log', true);
Configure::write('App.encoding', 'UTF-8');
Configure::write('App.baseUrl', 'components/com_cake/app');
//Configure::write('Routing.prefixes', array('admin'));
//Configure::write('Cache.disable', true);
//Configure::write('Cache.check', true);
define('LOG_ERROR', 2);
Configure::write('Session.save', 'php');
//Configure::write('Session.model', 'Session');
//Configure::write('Session.table', 'cake_sessions');
//Configure::write('Session.database', 'default');
Configure::write('Session.cookie', 'CAKEPHP');
Configure::write('Session.timeout', '120');
Configure::write('Session.start', true);
Configure::write('Session.checkAgent', true);
Configure::write('Security.level', 'medium');
Configure::write('Security.salt', 'd1a4bfb8a3cxxxxx47173663e9e2e9ea5');
Configure::write('Security.cipherSeed', '1269383xxxxxxxxxx3672219');
Configure::write('Asset.timestamp', 'force');
//Configure::write('Asset.filter.css', 'css.php');
//Configure::write('Asset.filter.js', 'custom_javascript_output_filter.php');
Configure::write('Acl.classname', 'DbAcl');
Configure::write('Acl.database', 'default');
//date_default_timezone_set('UTC');
Cache::config('default', array('engine' => 'File'));
This took me absolutely forever to figure out, but the problem was actually that I was using a CakePHP component inside the Joomla framework, and I had to select a different database using PHP’s native mysqli functions instead of Cake’s database abstraction layer.
What happened was that I changed the selected MySQL database, and this caused loads of subtle and seemingly unexplainable problems that came from Joomla but were only apparent in Cake.
The solution was to ensure that I switched back to the database that Joomla requires, in my
AppController::beforeRender()method.