title: Login with CSRF codeigniter and ajax
body: <p>i will make secure login with CSRF codeigniter and ajax. but i have a problem with my syntax. and $config['csrf_protection'] = TRUE;</p>
  
  <p>this my form :</p>
  
  <pre><code>&lt;?php echo form_open('admin/info_type_user_log/log_admin',array('id' =&gt; 'form-loginx'));?&gt;
          &lt;div class="input-group" style="margin-bottom:10px;"&gt;
              &lt;span class="input-group-addon lab"&gt;&lt;span class="glyphicon glyphicon-user"&gt;&lt;/span&gt;&lt;/span&gt;
              &lt;input type="text" name="username" id="username" class="form-control inp usernamex" placeholder="username" required&gt;
          &lt;/div&gt;
          &lt;div class="input-group" style="margin-bottom:10px;"&gt;
              &lt;span class="input-group-addon lab"&gt;&lt;span class="glyphicon glyphicon-lock"&gt;&lt;/span&gt;&lt;/span&gt;
              &lt;input type="password" name="password" id="password" class="form-control inp passwordx" placeholder="password" required&gt;
          &lt;/div&gt;
          &lt;button type="submit" name="submit" class="submit_login btn btn-md btn-primary"&gt;Login&lt;/button&gt;
          &lt;label&gt;&lt;input type="checkbox" class="lihat"&gt; lihat password&lt;/label&gt;
          &lt;?php echo form_close();?&gt;
  </code></pre>
  
  <p>and this my javascript :</p>
  
  <pre><code>$('#form-loginx').submit(function(e){ // Create `click` event function for login
          e.preventDefault();
          var csrfName = '&lt;?php echo $this-&gt;security-&gt;get_csrf_token_name(); ?&gt;',
              csrfHash = '&lt;?php echo $this-&gt;security-&gt;get_csrf_hash(); ?&gt;';
          var me = $(this);
          $('.submit_login').html('Loading ...'); //Loading button text 
  
          $.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
              url  : me.attr('action'),
              type : 'POST',
              data : {csrfName:csrfHash,me.serialize},
              dataType : 'json',
          success: function(response){ // Get the result and asign to each cases
              $('.submit_login').html('Login'); //Loading button text 
              if(response == true){
                  $(".alert-sukses").html("Sedang mengarahkan..").slideToggle("fast").delay(3000).slideToggle("fast");
                  window.location.href = 'admin/info_type_user_log';
              }else {
                  gagal();
              }
          }
          });
      });
  </code></pre>
  
  <p>and my controller :</p>
  
  <pre><code>function log_admin(){       
      $reponse = array('success' =&gt; true);
      $user = $this-&gt;security-&gt;xss_clean($this-&gt;input-&gt;post('username'));
      $pass = $this-&gt;security-&gt;xss_clean($this-&gt;input-&gt;post('password'));
  
       $reponse = array(
              'csrfName' =&gt; $this-&gt;security-&gt;get_csrf_token_name(),
              'csrfHash' =&gt; $this-&gt;security-&gt;get_csrf_hash()
              );
  
      $cek = $this-&gt;model_admst-&gt;valid_log($user, md5($pass));
  
      if($cek-&gt;num_rows() &gt; 0){
  
          foreach($cek-&gt;result() as $data){
              $sess_data['id']            = $data-&gt;id;
              $sess_data['nama_depan']    = $data-&gt;nama_depan;
              $sess_data['nama_belakang'] = $data-&gt;nama_belakang;
              $sess_data['email']         = $data-&gt;email;
              $sess_data['username']      = $data-&gt;username;
              $sess_data['gb_user']       = $data-&gt;gb_user;
              $sess_data['last_login']    = $data-&gt;last_login;
              $sess_data['log_access']    = "TRUE_OK_1";
  
              $this-&gt;session-&gt;set_userdata($sess_data);
              $this-&gt;model_admst-&gt;updateLastlogin($data-&gt;id);
              log_helper("login", "akses login sukses");
          }
  
          $reponse['success'] = true;
      }else{
          $reponse['success'] = false;
      }
      echo json_encode($reponse);
   }
  </code></pre>
  
  <p>and my models is :</p>
  
  <pre><code>function valid_log($user,$pass){
      $this-&gt;db-&gt;where('username', $user);
      $this-&gt;db-&gt;where('password', $pass);
      $this-&gt;db-&gt;where('status', 'aktif');
      $this-&gt;db-&gt;where('level', 'admjosslog21');
      $this-&gt;db-&gt;where('akses', '1');
      return $this-&gt;db-&gt;get('user');
  }
  </code></pre>