title: Password Validation in Codeigniter
body: <p>Hi all I have this issue when I validate log in in codeigniter that seems it does not check the required password in my database.The required password in my database is hash using this </p>
  
  <pre><code>$password_hash = password_hash($password, PASSWORD_BCRYPT);
  </code></pre>
  
  <p>I'm also using this hash to test of it's ability and security also.</p>
  
  <p>the code in my log in view is:</p>
  
  <pre><code>&lt;div class="container"&gt;
    &lt;div class="card card-login mx-auto mt-5"&gt;
      &lt;div class="card-header"&gt;Login&lt;/div&gt;
      &lt;div class="card-body"&gt;
        &lt;form method = "post" action=&lt;?php echo base_url("Ec_controller/login"); ?&gt; &gt;
          &lt;div class="form-group"&gt;
            &lt;label for="Username"&gt;Username&lt;/label&gt;
            &lt;input class="form-control" id="username" name="username" type="text" aria-describedby="emailHelp" placeholder="Enter Username"&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
            &lt;label for="Password"&gt;Password&lt;/label&gt;
            &lt;input class="form-control" id="password" name= "password" type="password" placeholder="Enter Password"&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
            &lt;div class="form-check"&gt;
              &lt;label class="form-check-label"&gt;
                &lt;!-- &lt;input class="form-check-input" type="checkbox"&gt; Remember Password&lt;/label&gt; --&gt;
            &lt;/div&gt;
          &lt;/div&gt;        
          &lt;input type="submit" name="submit" id="submit" class="btn btn-primary btn-xm" value="Log In" /&gt;
        &lt;/form&gt;
        &lt;div class="text-center"&gt;
          &lt;!-- &lt;a class="d-block small" href="#"&gt;Forgot Password?&lt;/a&gt; --&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>On my controller:</p>
  
  <pre><code>public function login(){
  
  
      $this-&gt;load-&gt;library('form_validation');
  
      $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'required|trim|callback_validate_credentials');
      $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required|trim');
  
      $username = $this-&gt;input-&gt;post('username');
      $password = $this-&gt;input-&gt;post('password');
      $user_id ="";
  
      if($this-&gt;form_validation-&gt;run()){
  
              $data = array(
                  'log_username' =&gt; $username,
                  'is_logged_in' =&gt;1
  
              );
              $this-&gt;session-&gt;set_userdata($data);
              $sql2 = $this-&gt;db-&gt;select("log_username, log_password,log_userlevel ")
                               -&gt;from("ec_login")
                               -&gt;where("log_username", $username)
                               -&gt;get();
  
  
  
              foreach($sql2-&gt;result() as $user_level){
  
                  $user_id = $user_level-&gt;log_userlevel;
  
              }
              if($user_id == 1){
  
                  redirect('Ec_controller/view_admin');
  
              }elseif ($user_id == 2) {
  
                  redirect('Ec_controller/view_it');
              }else{
  
                  redirect("Ec_controller/index");
              }
  
      }else{
  
          redirect('Ec_controller/index');
      }
  
  
  }
  
  public function validate_credentials(){
  
      $this-&gt;load-&gt;model('Ec_model');
  
      if($this-&gt;Ec_model-&gt;can_log_in()){
          return true;
      }else{
          $this-&gt;form_validation-&gt;set_message('validate_credentials', '&lt;font color=red&gt;Incorrect username/password&lt;/font&gt;');
          return false;
      }
  }
  </code></pre>
  
  <p>and on my Model:</p>
  
  <p>public function can_log_in(){   </p>
  
  <pre><code>$this-&gt;db-&gt;where('log_username', $this-&gt;input-&gt;post('username'));
  $this-&gt;db-&gt;where('log_password', password_verify($this-&gt;input-&gt;post('password'), PASSWORD_BCRYPT));       
  $query = $this-&gt;db-&gt;get('ec_login');
  
    if($query-&gt;num_rows() == 1)
     {        
       return true;
     }else{
        return false;
     }
  }
  </code></pre>
  
  <p>When I put username it validates the required username and the only problem is the password that whatever i put on the password it validated and redirect to specific page/views, it sounds crazy. A help and a little explanation would great help. </p>