2013-05-10 118 views
0

我開發了一個簡單的登錄系統,它工作正常但失敗了,我需要知道爲什麼。CI--告訴我出了什麼問題

問題:如何顯示導致失敗的原因?

這裏的數據庫功能:

function login($email,$password) 
{ 
    $this->db->where("email",$email); 
    $this->db->where("password",$password); 

    $query=$this->db->get("users"); 
    if($query->num_rows()>0) 
    { 
     foreach($query->result() as $rows) 
     { 
      //add all data to session 
      $newdata = array(
       'user_id' => $rows->id, 
       'user_name' => $rows->username, 
       'user_email' => $rows->email, 
       'logged_in' => TRUE, 
      ); 
     } 
     $this->session->set_userdata($newdata); 
     return true; 
    } 
    return false; 
} 

而這裏的邏輯:

public function login() 
{ 
    $this->load->library('form_validation'); 
    // field name, error message, validation rules 
    $this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 

    if($this->form_validation->run() == FALSE) 
    { 
     $this->signin(); 
    } 
    else 
    { 
     $email=$this->input->post('email'); 
     $password=md5($this->input->post('pass')); 
     $result=$this->user_model->login($email,$password); 
     if($result) 
     { 
      $this->dash(); 
     } 
     else 
     { 
      $data['title']= 'Login Error'; 
      $this->load->view('nav/header', $data); 
      $this->load->view('login', $data); 
      $this->load->view('nav/footer', $data); 
     } 
    } 
} 

我知道,因爲我重定向到登錄頁面,如果它失敗,更改標題文本的錯誤發生向我展示(僅適用於現在的測試模式)。但是,我怎樣才能找出哪裏出了問題?

這是檢查數據庫功能:

function login($email,$password) 
{ 
    $this->db->where("email",$email); 
    $this->db->where("password",$password); 

    $query=$this->db->get("users"); 
    if($query->num_rows()>0) 
    { 
     foreach($query->result() as $rows) 
     { 
      //add all data to session 
      $newdata = array(
       'user_id' => $rows->id, 
       'user_name' => $rows->username, 
       'user_email' => $rows->email, 
       'logged_in' => TRUE, 
      ); 
     } 
     $this->session->set_userdata($newdata); 
     return true; 
    } 
    return false; 
} 
+1

爲什麼你粘貼兩次型號代碼? – diEcho 2013-05-10 08:27:55

+0

備註:不要在修改密碼時使用'trim'規則 – diEcho 2013-05-10 08:42:47

+0

你完全改變了這個問題。問另一個,保留舊的。 – diEcho 2013-05-10 08:52:13

回答

0

寫下面的代碼在你的視圖文件

<section id="notification" > 
<?php 
     if(validation_errors() !== '') { 
     echo "<div class='alert-msg error'>"; 
     echo validation_errors(); 
     echo "</div>"; 
     } 
     $error = $this->session->flashdata('error'); 
     $success = $this->session->flashdata('success'); 
     if($error) 
     { 
     echo "<div class='alert-msg error'>"; 
     echo $this->session->flashdata('error'); 
     echo "</div>"; 
     } 

     if($success) 
     { 
     echo "<div class='alert-msg success'>"; 
     echo $this->session->flashdata('success'); 
     echo "</div>"; 
     } 
    ?> 
</section> 

和閃存數據控制器有條件地設置成功/錯誤信息(見以下)

if($result) { 
    $this->dash(); 
    $this->session->set_flashdata('success', 'Login successfully.'); 
 } else { 
     $this->session->set_flashdata('error', 'Login failed'); 
} 

更多Flashdata in CI

+0

謝謝但它沒有驗證錯誤 - 它的數據庫錯誤 - 將更新問題 – 2013-05-10 08:49:06

0

我假設你所有的PHP代碼是正確的,那麼你需要的是定製form-validation-message爲每個輸入知道哪個輸入出了錯,並且呼應他們:

<?php echo validation_errors(); ?> 
0

爲了您更改答案:

使用下面的邏輯模型中的

$qry = $this->db->get_where('users', array('username' => $this->_username)); 
if ($qry->num_rows() == 1) {   
    $user = $qry->row_array(); 
    $submitted_pass = md5($this->_password); 
    $db_pass = $user['password']; 
    if ($submitted_pass === $db_pass) { 
     return $user; 
    } else { 
     // wrong username/password 
     $this->session->set_flashdata('error', $this->errorList[10]); 
     return FALSE; 
    } 
} else { 
    // no such username exist  
    $this->session->set_flashdata('error', $this->errorList[15]); 
    return FALSE; 
}