2017-10-17 224 views
0

你好,我试图看看其他人,但无法找到我的基于他们的代码和解决方案不工作的原因。如果我禁用(注释掉)表单验证,我可以获取发布数据以显示。但如果留在它里面总是出现错误,并且不显示任何后期数据。我有自动加载我的模型,网址,表格,文本,安全,form_validationcodeigniter表单验证总是假的

在控制器

// Log in user 
     public function login(){ 
      $data['title'] = 'Sign In'; 

      $this->load->helper(array('form', 'url')); 

       $this->load->library('form_validation'); 


      // validate rules for login cedentials 
      $this->form_validation->set_rules('username', 'Username', 'required'); 
      $this->form_validation->set_rules('password', 'Password', 'required'); 

      // if login credentials are not entered (validation is false) show login view 
      if(!$this->form_validation->run()){ 
       $this->load->view('templates/header'); 
       $this->load->view('users/login', $data); 
       $this->load->view('templates/footer'); 
       echo "Not valid!"; 
       echo "<br>"; 
       print_r($this->input->post()); 
       echo $this->input->post('username'); 
       echo validation_errors(); 
       echo $this->form_validation->run(); 


      } else { 

       // Get username 
       $username = $this->input->post('username'); 
       // Get and encrypt the password 
       $password = $this->input->post('password'); 

       // Login user with model (database) 
       $user_id = $this->user_model->login($username, $password); 

       if($user_id){ 
        // Create session 
        $user_data = array(
         'user_id' => $user_id, 
         'username' => $username, 
         'logged_in' => true 
        ); 

        // set session user data 
        $this->session->set_userdata($user_data); 

        // Set message 
        $this->session->set_flashdata('user_loggedin', 'You are now logged in'); 

        redirect('users/index'); 
       } else { 
        // Set message 
        $this->session->set_flashdata('login_failed', 'Login is invalid'); 

        redirect('users/login'); 
       }  
      } 
     } // end function login 

鉴于形式

<?php echo form_open('users/login'); ?> <!-- the action is the controller users and the function register --> 
    <div class="row"> 
     <div class="col-md-4 col-md-offset-4"> 

      <div class="my-input-control-100p"> 
      <div class="form-group"> 
       <input type="text" name="username" class="form-control" placeholder="Enter Username" required autofocus> 
      </div> 
      </div> 

      <div class="form-group"> 
       <input type="password" name="password" class="form-control" placeholder="Enter Password" required autofocus> 
      </div> 

      <button type="submit" class="btn btn-primary btn-block">Login</button> 

      <br> 

      <div class="center_login"> 
       <small><a href="<?php echo site_url("pages/view/register") ?>">Register here</a> </small> 
       <br> 
       <br> 
       <small><a href="<?php echo site_url("pages/view/forgot") ?>">Forgot Password or User Name</a> </small> 
      </div> 
     </div> 
    </div> 
<?php echo form_close(); ?> 
+0

纯文本密码应该保存为密码验证程序。当保存密码验证者只使用哈希函数是不够的,只是添加一个盐没有提高安全性。相反,用随机盐迭代HMAC约100ms持续时间,然后用散列表保存盐。更好的是使用诸如'PBKDF2','Rfc2898DeriveBytes','password_hash','Bcrypt','passlib.hash'或类似函数的函数。关键是要让攻击者花费大量时间通过暴力破解密码。 – zaph

+0

与翻转的逻辑,我至少可以打印出用户名和密码(如果($ this-> form_validation-> run()!== false){) – tomcc84

+0

这是关于保护用户和他们的凭据,而不是开发人员的便利。永远不要在服务器上以纯文本保存密码。使用PHP使用['password_hash'](http://php.net/manual/en/function.password-hash.php)和['password_verify'](http://php.net/manual/en/function。 password-verify.php),这对安全且易于使用。 – zaph

回答

0

这是我得到了它作为工作的唯一途径它没有给一个布尔值,但布尔

如果数量表示($这个 - > form_validation->的run()!= 0){