How do you add a session message after session_write_close is called?
Question
We use the excellent uRapidFlow module by Unirgy, but have ran into a bit of an annoyance. When you run one of their profiles, they call session_write_close
. The problem with this is that it means if we wish to run a process from an adminhtml controller we can't set a session flash message to inform the user of success or caught exceptions.
Is it possible to restart the session after the process is complete, in order to add a session message?
I initially wasn't sure if it was possible to restart a session in PHP, but the proof of concept PHP works:
session_start();
// if this is the second run, show the value
if (!empty($_SESSION['foo'])) {
echo $_SESSION['foo'], PHP_EOL;
}
// set an initial value
$_SESSION['foo'] = 'bar';
// start running profile
session_write_close();
// after running profile
session_start();
// set a value to see if it takes
$_SESSION['foo'] = 'foo';
I haven't been able to correctly set a flash message on the adminhtml session though. I thought I might be able to call something along the lines of:
Mage::getSingleton('adminhtml/session')
->init('core', 'adminhtml')
->addSuccess('Whoop');
But this doesn't seem to work.
Solution
This should work:
unset($_SESSION);
Mage::getSingleton('adminhtml/session')
->init('core', 'adminhtml')
->addSuccess('Whoop');
The session isn't lost after session_write_close()
, if you want to write to the session again you would have to reopen it though. Proof of concept:
<?php
session_start();
// if this is the second run, show the value
if (!empty($_SESSION['foo'])) {
echo $_SESSION['foo'], PHP_EOL;
}
// set an initial value
$_SESSION['foo'] = 'bar';
// start running profile
session_write_close();
// after running profile
session_start();
var_dump($_SESSION);
//// set a value to see if it takes
$_SESSION['test'] = 'foo';