2010-06-09 78 views
1

出於某種原因,用戶可以使用任何密碼登錄,開始我還以爲我忘了檢查密碼,但我沒有......我只是不能找到問題笨:驗證問題

這裏是型號:

/*#######################################################*/ 
    function validate() 
/*#######################################################*/ 
    { 
     $this->db->where('username', $this->input->post('username')); 
     $this->db->where('password', md5($this->input->post('password'))); 
     $q = $this->db->get('user_extra'); 

     if($q->num_rows() == 1): 
      return true; 
     else: 
      return false; 
     endif; 
    }//end of function validate() 

/*#######################################################*/ 
     function validate_credentials() 
/*#######################################################*/ 
     { 
      $this->load->model('membership_model'); 

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

      $this->form_validation->set_rules('username', 'Name', 'trim|required'); 
      $this->form_validation->set_rules('password', 'password', 'trim|required'); 
      if(!$this->membership_model->validate()): 
       $this->form_validation->set_message('check_login', 'Login not correct, please try again.'); 
      endif; 

      if($this->form_validation->run() == FALSE): 
       $this->index(); 
      else: 
       $this->membership_model->userinfo($this->input->post('username')); 
       //should redirect to last view 
       redirect($this->session->flashdata('redirect_url')); 
      endif; 
     }// end of validate_credentials() 

回答

0

我試圖與回調函數,但它並沒有出於某種原因,所以我才搬進來的其他語句下面的if語句驗證 - >運行(),這是代碼:

$this->form_validation->set_rules('username', 'Name', 'trim|required|xss_clean'); 
      $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean'); 


      if($this->form_validation->run() == FALSE): 
       redirect(base_url().'login/index/error'); 
      else:  
       if($this->membership_model->validate()): 
        $this->membership_model->userinfo($this->input->post('username')); 
        //should redirect to last view 
        redirect('home/index'); 
       else: 
        redirect(base_url().'login/index/error'); 
       endif; 
      endif; 
0

你爲什麼不建立在驗證串回調函數控制器?

將這個功能在同一個控制器

/*##############PRIVATE FUNCTION WITHIN THE SAME CONTROLLER###########*/ 
    function validate($username,$password) //if you use php 5 i would set this method to private one 
/*#######################################################*/ 
    { 
     $this->db->where('username', $username); 
     $this->db->where('password', md5($password)); 
     $q = $this->db->get('user_extra'); 

     if($q->num_rows() <= 1) 
      return true; 
     return false; 

    }//end of function validate() 

和規則行內:

$this->form_validation->set_rules('username', 'Name', 'trim|required|callback_validaion[password]'); 
+0

callback_validaion [密碼]在set_rules和你的名字控制器函數validate(),它應該驗證()??? 無論如何,我試過,但你只傳遞密碼,你確定它會找到用戶名?除了在控制器中添加查詢外,它應該位於模型中。但謝謝你的迴應 – Christophe 2010-06-10 09:56:13

+0

哦,對於我以前的評論抱歉,我明白你的意思,我的壞。 – Christophe 2010-06-10 09:59:27

0

爲什麼不直接使用caalback作爲表單驗證的一部分?

例如

$this->form_validation->set_rules('password', 'Password', 'prep_for_form|required|xss_clean|callback_password_check'); 

和回調函數...

function password_check($value) { /* call model and query database to get password */ if ($value == $password_from_model)) {  return TRUE; } else {  $this->form_validation->set_message('password_check', 'You have not used the correct %s.');   return FALSE; } } 
+0

我做了,但沒有工作 – Christophe 2010-06-09 13:02:49

+0

對不起,我不好。忘記添加等於回撥的情況! – Rooneyl 2010-06-09 13:50:27