9

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.

  • If you want to show messages to customer during profile execution, you may use $pofile->getLogger() to get instance of logger and pass messages to it. It is not the same as showing them in the page, but you will have record of them – Petar Dzhambazov Jan 25 '13 at 18:18
4

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';
  • Calling init on the session won't trigger session_start to be called, since it is only called if $_SESSION isn't set (which it seems to be after calling session_write_close). – Peter O'Callaghan Sep 4 '13 at 10:57
  • For anybody interested in doing this, in order to make it function correctly you have to unset($_SESSION). After doing this init will reload the session correctly. – Peter O'Callaghan Nov 29 '13 at 1:18
  • @Cags I have updated my answer, can you check if it's correct? – Rick Kuipers Dec 2 '13 at 13:28
  • The session_start isn't required as it gets called as part of the init method call (well in start which is called by init). Other than that, it should work. – Peter O'Callaghan Dec 2 '13 at 14:02
  • @Cags Thanks, I updated my answer! I appreciate you coming back with feedback :) – Rick Kuipers Dec 2 '13 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.