title: codeigniter sessions not carrying from page to page
body: <p>Is anyone here familiar with CodeIgniter sessions? 
  I have a login script which is functional. On a successful login, a session is created. A user is redirected to thier home page. The new page should show thier username as it was stored in the session variables, but shows nothing at all. </p>
  
  <p>The code from the login script:</p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Login extends CI_Controller {
  
      /**
       * Index Page for this controller.
       */
      public function index() {
          $this-&gt;load-&gt;library('session');
          $BB_DATA = array();
          if(isset($_POST['login'])) {
              // SELECT username, password FROM Bacon or bacon-like product. &lt;-- Fuck you #nobanchancom
              if(!empty($_POST['username']) &amp;&amp; !empty($_POST['password'])) {
                  $q = sprintf('SELECT user_pk, username, pwhash, salt FROM tbl_user WHERE `username`=%s LIMIT 1;', $this-&gt;db-&gt;escape($_POST['username']));
                  $records = $this-&gt;db-&gt;query($q);
                  $username = $_POST['username']; // i added this
                  if($records-&gt;num_rows() == 1) {
                      $record = $records-&gt;row();
                      $test_hash = hash_password($record-&gt;salt . $_POST['password']);
                      if($record-&gt;pwhash === $test_hash) {
                          $new_salt = generate_salt();
                          $new_hash = hash_password($new_salt . $_POST['password']);
                          $q = sprintf('UPDATE tbl_user SET `pwhash`="%s", `salt`="%s" WHERE `user_pk`="%s" LIMIT 1;', $new_hash, $new_salt, $record-&gt;user_pk);
                          if($updates = $this-&gt;db-&gt;query($q)) {
                              $BB_DATA['messages']['information'][] = "Login successful.";
  
                              //add custom data to session
                              $this-&gt;session-&gt;set_userdata('username', "$username");
  
                              //$this-&gt;session-&gt;set_userdata('some_name', 'some_value');
                              // redirect to /users/home/
                              redirect(site_url() . 'users/home/'); exit();
                          } else {
                              $BB_DATA['messages']['critical'][] = "You have logged in successfully, but an error has occurred during login.";
                          }
                      } else {
                          $BB_DATA['messages']['critical'][] = "That username/password does not match.";
                      }
                  } else {
                      $BB_DATA['messages']['warning'][] = "That username/password does not exist.";
                  }
              } else {
                  $BB_DATA['messages']['warning'][] = "You must enter a username/password.";
              }
          }
          if(isset($_POST['register'])) {
              // Redirect to /register/
              redirect(site_url() . 'register/');
          }
          $this-&gt;load-&gt;view('login', $BB_DATA);
      }
  }
  
  /* End of file login.php */
  /* Location: ./application/controllers/login.php */
  </code></pre>
  
  <p>And then here is the code for the users landing page: </p>
  
  <pre><code>&lt;?php
  class Users extends CI_Controller {
  
      public function home()
      {
          $this-&gt;session-&gt;userdata('username');
          require_once(APPPATH . 'views/header.inc.php');//header
              include(APPPATH . 'views/breadcrumbs.inc.php');//top breadcrumbs
  
              echo ' &lt;h1&gt;Welcome'.$username.',&lt;/h1&gt;';
              //$username = $this-&gt;session-&gt;userdata('username');
              echo $this-&gt;session-&gt;all_userdata();
              //echo $username;
              include(APPPATH . 'views/breadcrumbs.inc.php');//bottom breadcrumbs
              require_once(APPPATH . 'views/footer.inc.php');//footer
      }
  
  
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>In the login script, I call codeigniters session library- </p>
  
  <blockquote>
    <p>$this->load->library('session');</p>
  </blockquote>
  
  <p>later, assuming everything went right, I add the $username variable to the session data. The user is redirected, and from there </p>
  
  <blockquote>
    <p>$this->session->userdata('username');
    should re establish the variable. It does not. </p>
  </blockquote>
  
  <p>Suggestions?</p>
  
  <p>NOTE: So, I used <code>print_r(array_values($this-&gt;session-&gt;all_userdata()));</code> to print all the values in the array. It contained: </p>
  
  <blockquote>
    <p>Array ( [0] => 4c46f32b249ed55e8439152d63365fed [1] => 75.134.164.245
    [2] => Mozilla/5.0 (Windows NT; Win64; x64; rv:26.0) Gecko/20100101
    Firefox/26.0 Waterfox/26.0 [3] => 1410568024 [4] => [5] => ottomatic )</p>
  </blockquote>
  
  <p>So I am curious, Why did it add it as [5] rather than 'username'?</p>