title: How do I create, write, and read session data in CakePHP?
body: <p>can anyone give me an example on how to create Sessions and write data to it. I've seen syntax on how to write data to a session using write command. But how to create a session and retrieve the values in it.</p>
  
  <p>In my application, I have two data, form_id and user_id that needs to be used in all the page requests. So how do I save it as a session variable and use it across the application?</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>function register()
  {
      $userId=$this-&gt;User-&gt;registerUser($this-&gt;data);
      $this-&gt;Session-&gt;write('User.UserId',$userId);
      //echo "session".$this-&gt;Session-&gt;read('User.UserId');
      $this-&gt;User-&gt;data=$this-&gt;data;
      if (!$this-&gt;User-&gt;validates())
      {
      $this-&gt;Flash('Please enter valid inputs','/forms' );
        return;   
      }
  
  $this-&gt;Flash('User account created','/forms/homepage/'.$userId);            
  
  }   
  </code></pre>
  
  <p>How to use the session variable <strong>'User.UserId'</strong> instead of $userId in $this->Flash('User account created','/forms/homepage/'.<strong>$userId</strong>);</p>
  
  <p>And can I use this variable in all my view files,because in all the page requests I also pass the userId?</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I have 2 controllers,user and form. I write the userid to a session variable in the users<em>_controller. I have a view file called homepage.ctp,whose action is in the forms_controller.</em> Now how can I use the session variable defined in the users_controller in the homepage? Sorry if I am asking silly questions. I went through the cakebook,but my doubts weren't cleared. I'm also trying trial and error method of coding,so please help me.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>I have a session variable 'uid' which is the user id in the home page action of a controller.</p>
  
  <pre><code>       $this-&gt;Session-&gt;write('uid',$this-&gt;data['Form']['created_by']);
  </code></pre>
  
  <p>I need the same variable in the design action method of the same controller.
  When I give   </p>
  
  <pre><code>             $uid=$this-&gt;Session-&gt;read('uid');
              echo "uid: ".$uid;
  </code></pre>
  
  <p>the value is not echoed.</p>
  
  <p>Can't I use the session variable in the same controller?</p>