title: Codeigniter session is not saving userdata and creating duplicate sessions
body: <p>I'm trying to send my <em>username</em> and <em>password</em> via AJAX to the database for verifying the user but I see CI is creating duplicate sessions for a single request. Also when I'm trying to set the <code>userdata</code> through <code>session-&gt;set_userdata</code> it is not setting the data. This is confirmed as neither firebug nor my session database shows the data. Also since no data is getting set in the session I do not get anything back in my AJAX response to show in the AJAX response success</p>
  
  <p>Please guide me what am I doing wrong here as the session cookies are created even if the <em>username</em> and <em>password</em> combination is wrong.</p>
  
  <p>Model:</p>
  
  <pre><code>function validate_login()
  {
      $this-&gt;db-&gt;where('Useremail',$this-&gt;input-&gt;post('username'));
      $this-&gt;db-&gt;where('Password',$this-&gt;input-&gt;post('password'));
      $result = $this-&gt;db-&gt;get('users');
      if ($result-&gt;num_rows == 1) {
          return true;
      }else{
          return false;
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>function validate_login_user()
  {
      $this-&gt;load-&gt;model('data_model');
  
      if ($this-&gt;data_model-&gt;validate_login())
      {
          $data = array (
              'username' =&gt; $this-&gt;input-&gt;post('username'),
              'is_logged_in' =&gt; true
          );
          $this-&gt;session-&gt;set_userdata($data);
          return = $this-&gt;session-&gt;userdata('username');
      }
      else
      {
          return false;
      }
  }
  </code></pre>
  
  <p>My view:</p>
  
  <pre><code>&lt;script&gt;
  
      $(function() {
  
          $('.login').click(function(){
              var username = $('#username').attr('value');
              var password = $('#password').attr('value');
              if (username == "" || password == ""){
                  $('.LoginErrormsg').css('display','block');
              }
              if (username !== "" &amp;&amp; password !== ""){
                  $.ajax({
                      url:"&lt;?php echo site_url('home/validate_login_user');?&gt;",
                      type:'POST',
                      data:{username:username,
                          password:password},
                      success:function(){
                          $('#sitelogin').html(msg);
                          $('.logout').css('display','block');
                      }
                  });
                  return false
              }else{
                  return false;
              }
          });
      })
  
  &lt;/script&gt;
  
  &lt;div id="sitelogin"&gt;
      &lt;input type="text" class="loginbox" name = "login_username" id = "username" placeholder="Email address / Username" /&gt;
      &lt;input type="password" class="loginbox" name = "login_password" id="password" placeholder="Password" /&gt;
      &lt;div class="LoginErrormsg"&gt;You forgot to enter Username and Password&lt;/div&gt;
          &lt;button class="login" name = "login"&gt;Login&lt;/button&gt;
          &lt;button class="logout" name = "logout"&gt;Logout&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>