Is there any way to save a variable in the Craft session inside a controller?
I'm running a cron job and I need to store a variable as a session for every run to see if a condition matches.
Is there any way to save a variable in the Craft session inside a controller?
I'm running a cron job and I need to store a variable as a session for every run to see if a condition matches.
Craft's HttpSessionService is a thin wrapper for PHP's global $_SESSION variable.
You can use:
craft()->httpSession->add($key, $value)
and:
craft()->httpSession->get($key)
to save and retrieve values from it.
HttpSessionVariable
class where you'd be able to access that. Hit us up at support@buildwithcraft.com if you think you have a valid use-case where you think that'd be necessary from a template.
– Brad Bell
Nov 5 '14 at 5:29
And for those needing this for Craft 3...
Set (previously add):
Craft::$app->getSession()->set($key, $value);
Get
Craft::$app->getSession()->get($key);