title: Session variable in Codeigniter Not Working
body: <p>Hi i have created a session in Codeigniter i have set the session variable in my model. But after setting the session variable value when i call session variable in my view through Controller the session variable value becomes null. Any help???</p>
  
  <p><strong>Update</strong></p>
  
  <p>This is my Model where i set my session variable</p>
  
  <pre><code>function login ($username, $password)
  
      {
          $this-&gt;db-&gt;where('username' , $username);
          $this-&gt;db-&gt;where('password', $password);
  
          $query = $this-&gt;db-&gt;get('users');
  
          if ($query-&gt;num_rows()&gt;0)
  
          {
              foreach ($query-&gt;result() as $rows)
              {
                  $data = array(
  
                      'user_name' =&gt; $rows-&gt;username,
                      'logged_in' =&gt; TRUE,
                      'validated' =&gt; true
                  );
              }
  
                  $this-&gt;session-&gt;set_userdata($data);
                  //$user = $rows-&gt;username;
                  //$this-&gt;session-&gt;set_userdata('user_name', $user);
  
                  return true;
          }
  
          else
  
          {
              return false;
          }
  
      }
  </code></pre>
  
  <p>Here is my controller from where i redirect to the view</p>
  
  <pre><code>public function verification()
  
      {
          $username = $this-&gt;input-&gt;post('username');
          $password = $this-&gt;input-&gt;post('password');
  
  
          $result = $this-&gt;site_model-&gt;login($username, $password);
  
  
          //$result = $this-&gt;session-&gt;set_userdata('validated');
  
          if ($result)
          {
              //$this-&gt;admin();
              //$this-&gt;session-&gt;set_userdata('login_state', TRUE);
  
              redirect ('site/index');
  
          }
          else
          {
  
              redirect ('site/login');
              //$this-&gt;load-&gt;view('login');
          }
      }
  </code></pre>
  
  <p>i have called the <code>session_start();</code> in controller under <code>construct();</code></p>
  
  <p>and this is how i access the session variable in my view </p>
  
  <pre><code>&lt;?php if ($this-&gt;session-&gt;userdata('user_name') != "") { ?&gt;
  
  .....
  </code></pre>