Answers.Body: <p>In addition to passing the value along through a <a href="https://craftcms.stackexchange.com/a/10791/57">cookie</a>, you could also do it through PHP's session support.</p>
  
  <p>Adding:</p>
  
  <pre><code>craft()-&gt;httpSession-&gt;add('sessionId', '123456');
  </code></pre>
  
  <p>Retrieving:</p>
  
  <pre><code>$sessionId = craft()-&gt;httpSession-&gt;get('sessionId');
  </code></pre>
  
  <p>Deleting:</p>
  
  <pre><code>craft()-&gt;httpSession-&gt;remove('sessionId');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T16:54:04.113Z
Answers.LastActivityDate: 2017-03-28T20:37:19.703Z
Answers.LastEditDate: 2017-04-13T12:51:00.160Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>You can use cookies from craft to store / retrieve and delete the session id. Craft checks if the value isn't tampered with and hashed the value.</p>
  
  <p>Creating a cookie:</p>
  
  <pre><code>craft()-&gt;userSession-&gt;saveCookie('session_id', $this-&gt;session_id, $duration);
  </code></pre>
  
  <p>Where duration is in seconds, don't know what happens if you use 0.</p>
  
  <p>For retrieving the cookie value</p>
  
  <pre><code>$sessionId = craft()-&gt;userSession-&gt;getStateCookieValue('sessionId');
  </code></pre>
  
  <p>If you want to delete the cookie in the final step.</p>
  
  <pre><code>craft()-&gt;userSession-&gt;deleteStateCookie('session_id');
  </code></pre>
  
  <p>Good luck!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T16:34:52.480Z
Answers.Text: If you set `$duration` to 0 (or just don't pass it as a parameter), it's a browser session based cookie and will expire when the browser is closed.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T16:48:08.137Z
Answers.Text: Also, I'd probably go through HttpRequest's cookie methods instead of UserSessionService's.  The latter deals specifically with the identity cookie that Craft uses when logging in and for session management. See: http://www.yiiframework.com/wiki/152/cookie-management-in-yii/ and just replace `Yii::app()->request` with `craft()->request`.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T11:32:26.590Z
Answers.LastActivityDate: 2015-07-31T16:42:27.123Z
Answers.LastEditDate: 2015-07-31T16:42:27.123Z
Answers.UserDisplayName: Pim Kaptein
Body: <pre><code>namespace Craft;
  
  class Test_FormsController extends BaseController
  {
  
     protected $allowAnonymous = true;
     public $session_id;
  
      public function actionApply(){
          $appl_template = "1";
          $this-&gt;session_id = 15558145;
          return $this-&gt;renderTemplate("apply/step".$appl_template.".html");
      }
  
      public function actionSaveDataStruct(){
          echo 'session:'.$this-&gt;session_id.'&lt;br /&gt;';
          exit();
      }
  }
  </code></pre>
  
  <p>Hi guys i'm newbie on Craft and my question is:</p>
  
  <p><code>acctionApply</code> loads form which will send to <code>actionSaveDataStruct</code>, I set a class variable <code>$session_id</code> on <code>actionApply</code> but whenever the form submitted and goes to <code>actionSaveDataStruct</code>, <code>$this-&gt;session_id</code> become null again, how do I set variable that will never reset whenever the page load?</p>
  
Tags: <plugin-development>
Title: How to store class variable inside a Craft controller
ThreadText: # How to store class variable inside a Craft controller
  
  (Asked by: username_0 on 2015-07-31)
  
  ```
  namespace Craft;
  
  class Test_FormsController extends BaseController
  {
  
     protected $allowAnonymous = true;
     public $session_id;
  
      public function actionApply(){
          $appl_template = "1";
          $this->session_id = 15558145;
          return $this->renderTemplate("apply/step".$appl_template.".html");
      }
  
      public function actionSaveDataStruct(){
          echo 'session:'.$this->session_id.'<br />';
          exit();
      }
  }
  
  ```
  
  Hi guys i'm newbie on Craft and my question is:
  
  `acctionApply` loads form which will send to `actionSaveDataStruct`, I set a class variable `$session_id` on `actionApply` but whenever the form submitted and goes to `actionSaveDataStruct`, `$this->session_id` become null again, how do I set variable that will never reset whenever the page load?
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-07-31. Score: 2
  
  In addition to passing the value along through a [cookie](https://craftcms.stackexchange.com/a/10791/57), you could also do it through PHP's session support.
  
  Adding:
  
  ```
  craft()->httpSession->add('sessionId', '123456');
  
  ```
  
  Retrieving:
  
  ```
  $sessionId = craft()->httpSession->get('sessionId');
  
  ```
  
  Deleting:
  
  ```
  craft()->httpSession->remove('sessionId');
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2015-07-31. Score: 1
  
  You can use cookies from craft to store / retrieve and delete the session id. Craft checks if the value isn't tampered with and hashed the value.
  
  Creating a cookie:
  
  ```
  craft()->userSession->saveCookie('session_id', $this->session_id, $duration);
  
  ```
  
  Where duration is in seconds, don't know what happens if you use 0.
  
  For retrieving the cookie value
  
  ```
  $sessionId = craft()->userSession->getStateCookieValue('sessionId');
  
  ```
  
  If you want to delete the cookie in the final step.
  
  ```
  craft()->userSession->deleteStateCookie('session_id');
  
  ```
  
  Good luck!
  
  > **Comments:**
  > **username_1**: If you set `$duration` to 0 (or just don't pass it as a parameter), it's a browser session based cookie and will expire when the browser is closed.  
  > **username_1**: Also, I'd probably go through HttpRequest's cookie methods instead of UserSessionService's.  The latter deals specifically with the identity cookie that Craft uses when logging in and for session management. See: http://www.yiiframework.com/wiki/152/cookie-management-in-yii/ and just replace `Yii::app()->request` with `craft()->request`.  
  
  ---
  
  