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 />';

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?

2 Answers 2


In addition to passing the value along through a cookie, you could also do it through PHP's session support.


craft()->httpSession->add('sessionId', '123456');


$sessionId = craft()->httpSession->get('sessionId');



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.


Good luck!

  • 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
    Jul 31, 2015 at 16:34
  • 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: yiiframework.com/wiki/152/cookie-management-in-yii and just replace Yii::app()->request with craft()->request.
    – Brad Bell
    Jul 31, 2015 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.