<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>

CC BY-SA 3.0
2015-07-31T16:54:04.113Z
2017-03-28T20:37:19.703Z
2017-04-13T12:51:00.160Z
Brad Bell
<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>

CC BY-SA 3.0
2015-07-31T16:34:52.480Z
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.
Brad Bell
CC BY-SA 3.0
2015-07-31T16:48:08.137Z
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`.
Brad Bell
CC BY-SA 3.0
2015-07-31T11:32:26.590Z
2015-07-31T16:42:27.123Z
2015-07-31T16:42:27.123Z
Pim Kaptein
<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>

<plugin-development>
How to store class variable inside a Craft controller
# 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.'';
        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, 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`.  

---

