<p>And for those needing this for Craft 3...</p>

<p><strong>Set (previously called 'add'):</strong> </p>

<pre><code>Craft::$app-&gt;getSession()-&gt;set($key, $value);
</code></pre>

<p><strong>Get</strong></p>

<pre><code>Craft::$app-&gt;getSession()-&gt;get($key);
</code></pre>

CC BY-SA 4.0
2018-08-29T14:19:30.263Z
If you need to unset/remove a particular session variable key:

**Remove**
Craft::$app->getSession()->remove($key);

[Here are the applicable Yii2 docs](https://www.yiiframework.com/doc/api/2.0/yii-web-session).
plaintxt
CC BY-SA 4.0
2018-02-15T00:03:21.593Z
2020-04-09T10:04:55.177Z
2020-04-09T10:04:55.177Z
Mark Notton
<p>Craft's <a href="http://buildwithcraft.com/classreference/services/HttpSessionService">HttpSessionService</a> is a thin wrapper for PHP's global $_SESSION variable.</p>

<p>You can use:</p>

<pre><code>craft()-&gt;httpSession-&gt;add($key, $value)
</code></pre>

<p>and:</p>

<pre><code>craft()-&gt;httpSession-&gt;get($key)
</code></pre>

<p>to save and retrieve values from it.</p>

CC BY-SA 3.0
2014-11-05T04:28:32.530Z
Brad - How would you grab the key in a twig template?
Adam McCombs
CC BY-SA 3.0
2014-11-05T05:29:13.990Z
We don't expose a `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
CC BY-SA 3.0
2014-11-05T05:35:24.867Z
Gotcha. I actually got what I needed from setFlash instead of httpSession. Thanks.
Adam McCombs
CC BY-SA 3.0
2014-09-08T16:57:33.527Z
2014-09-08T16:57:33.527Z
Brad Bell
<p>Is there any way to save a variable in the Craft session inside a controller?</p>

<p>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.</p>

<plugin-development><session>
Can you save a variable in session?
# Can you save a variable in session?

(Asked by: username_0 on 2014-09-08)

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.

**Tags:** plugin-development, session


---

## 2 answers

### Answer by username_3 on 2014-09-08. Score: 12 | Accepted answer

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.

> **Comments:**
> **username_4**: Brad - How would you grab the key in a twig template?  
> **username_3**: We don't expose a `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.  
> **username_4**: Gotcha. I actually got what I needed from setFlash instead of httpSession. Thanks.  

---

### Answer by username_1 on 2018-02-15. Score: 13

And for those needing this for Craft 3...
Set (previously called 'add'): 
Craft::$app->getSession()->set($key, $value);

Get
Craft::$app->getSession()->get($key);

> **Comments:**
> **username_2**: If you need to unset/remove a particular session variable key:  **Remove** Craft::$app->getSession()->remove($key);  [Here are the applicable Yii2 docs](https://www.yiiframework.com/doc/api/2.0/yii-web-session).  

---

