Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-29T14:19:30.263Z
Answers.Text: 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).
Answers.UserDisplayName: plaintxt
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-02-15T00:03:21.593Z
Answers.LastActivityDate: 2020-04-09T10:04:55.177Z
Answers.LastEditDate: 2020-04-09T10:04:55.177Z
Answers.UserDisplayName: Mark Notton
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T04:28:32.530Z
Answers.Text: Brad - How would you grab the key in a twig template?
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T05:29:13.990Z
Answers.Text: 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.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T05:35:24.867Z
Answers.Text: Gotcha. I actually got what I needed from setFlash instead of httpSession. Thanks.
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-08T16:57:33.527Z
Answers.LastActivityDate: 2014-09-08T16:57:33.527Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development><session>
Title: Can you save a variable in session?
ThreadText: # 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](http://buildwithcraft.com/classreference/services/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**: username_3 - 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).  
  
  ---
  
  