2016-11-17 42 views
0

我正在關注本頁中的教程http://www.kodingmadesimple.com/2016/06/codeigniter-login-and-registration-tutorial-source-code.html和我和其他人發現了相同的錯誤。:codeigniter登錄表單驗證總是失敗

它在登錄控制器中出現錯誤,因爲它總是顯示「錯誤的電子郵件ID或密碼!」消息,即使證書沒有問題,但我找不到錯誤(儘管我找到並解決了其他問題),請幫助我。

控制器:在視圖

<?php 
class login extends CI_Controller 
{ 
public function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('form','url','html')); 
    $this->load->library(array('session', 'form_validation')); 
    $this->load->database(); 
    $this->load->model('user_model'); 
} 
public function index() 
{ 
    // get form input 
    $email = $this->input->post("email"); 
    $password = $this->input->post("password"); 
    //(This was added later for me, as it was needed) 
    $this->load->helper('security'); 
    // form validation 
    $this->form_validation->set_rules("email", "Email-ID", "trim|required|xss_clean"); 
    $this->form_validation->set_rules("password", "Password", "trim|required|xss_clean"); 

    if ($this->form_validation->run() == FALSE) 
    { 
     // validation fail 
     $this->load->view('login_view'); 
    } 
    else 
    { 
     // check for user credentials 
     $uresult = $this->user_model->get_user($email, $password); 
     if (count($uresult) > 0) 
     { 
      // set session 
      $sess_data = array('login' => TRUE, 'uname' => $uresult[0]->fname, 'uid' => $uresult[0]->id); 
      $this->session->set_userdata($sess_data); 
      redirect("profile/index"); 
     } 
     else 
     { 
      $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Wrong Email-ID or Password!</div>'); 
      redirect('login/index'); 
     } 
    } 
    } 
} 

相關的部分:

<?php $attributes = array("name" => "loginform"); 
     echo form_open("login/index", $attributes);?> 
     <legend>Login</legend> 
     <div class="form-group"> 
      <label for="name">Email-ID</label> 
      <input class="form-control" name="email" placeholder="Enter Email-ID" type="text" value="<?php echo set_value('email'); ?>" /> 
      <span class="text-danger"><?php echo form_error('email'); ?></span> 
     </div> 
     <div class="form-group"> 
      <label for="name">Password</label> 
      <input class="form-control" name="password" placeholder="Password" type="password" value="<?php echo set_value('password'); ?>" /> 
      <span class="text-danger"><?php echo form_error('password'); ?></span> 
     </div> 
     <div class="form-group"> 
      <button name="submit" type="submit" class="btn btn-info">Login</button> 
      <button name="cancel" type="reset" class="btn btn-info">Cancel</button> 
     </div> 
    <?php echo form_close(); ?> 
    <?php echo $this->session->flashdata('msg'); ?> 

,如果我沒有解釋清楚自己,或者如果你需要看到更多的代碼,你可以直接檢查的教程鏈接。

您的幫助將非常感謝,謝謝。

編輯: 這是在模型的功能:

function get_user($email, $pwd) 
{ 
    $this->db->where('email', $email); 
    $this->db->where('password', md5($pwd)); 
    $query = $this->db->get('user'); 
    return $query->result(); 
} 

我已經改變「PWD」爲「密碼」並刪除了MD5,因爲我不需要它,現在它的工作。

+0

'如果(計數($ uresult)> 0)'等於零,你需要看一下'get_user'函數,看看那裏發生了什麼。另外,我認爲你有數據庫中的用戶是啊? – Blinkydamo

+0

數據庫連接和數據都可以,因爲我可以添加新用戶 – NullEins

+0

好的,但結果數仍然爲零,因此它爲什麼會進入else並給出錯誤。 – Blinkydamo

回答

0

因爲你得到這條線的形式驗證OK:

$uresult = $this->user_model->get_user($email, $password); 
     if (count($uresult) > 0) 

檢查GET_USER功能,以確保您正在返回的結果集,即:

$this->db->where('user_email', $user_email); 
$result = $this->db->get('users'); 

return $result->result(); 
+0

感謝您指向正確的方向,函數中的變量名稱不同。現在它的工作! – NullEins

+0

不用擔心 - 很高興這是一個簡單的修復! –