2017-09-27 18 views
0

我是codeigniter的新手,我試圖登錄後登錄...我輸入正確的用戶名和密碼,但錯誤是「在數據庫中不存在這樣的帳戶「,如果該消息爲假,則在else部分中設置消息。我註冊後無法登錄(雖然檢查:回聲num_rows()爲0,雖然有記錄)

檢查我用echo num_rows()它顯示0(這是錯誤的),共有5條記錄在那裏,但echo num_fields顯示8個字段(這是正確的)。

控制器登錄代碼:

public function login() { 
    $this->form_validation->set_rules('username', 'User Name', 'required'); 
    $this->form_validation->set_rules('password', 'Password', 'required'); 
    if ($this->form_validation->run() === FALSE) { 
     $this->load->view('login'); 
    } else { 
    //get user name 
    $username = $this->input->post('username'); 
    //get encrypt password 
    $password = md5($this->input->post('password')); 

    //login user 
    $user_id = $this->auth_model->login($username, $password); 
    if ($user_id) { 
     //create session 
     $user_data = array(
      'user_id' => $user_id, 
      'username' => $username, 
      'logged_in' => true 
     ); 
     $this->session->set_userdata($user_data); 
     //set message 
     $this->session->set_flashdata("success", "your are logged in"); 
     redirect('index.php/users/profile'); 
     } else { 
      $this->session->set_flashdata("error", "No such account exists in database"); 
      redirect('index.php/auth/login'); 
     } 
    } 
} 

模型登錄代碼:

public function login($username, $password) { 

    $this->db->where('username', $username); 
    $this->db->where('password', $password); 
    $result = $this->db->get('tblLogin'); 

    if ($result->num_rows() == 1) { 
     return $result->row(0)->id; 
    } else { 
     return false; 
    } 
} 
+0

不要在密碼上使用md5! – Twinfriends

+0

thanq,是的,我知道了..但如何加密密碼?不需要它加密密碼 –

+0

試試這個[教程](https://www.sitepoint.com/hashing-passwords-php-5-5-password-hashing-api/)。你是否也試過用你的手來檢查sql是否返回任何內容? – Vastlik

回答

0
public function login($username, $password) { 

    $this->db->where('username', $username); 
    $this->db->where('password', $password); 
    $result = $this->db->get('tblLogin'); 
    $ret = $result->row(); 

    if ($ret) { 
     return $ret; 
    } else { 
     return false; 
    } 
} 

行()函數得到的是單一的,如果條件辦理入住手續。 使用$this->db->last_query(); 打印您的查詢並檢查您的mysql數據庫。

0

你可以試試這個解決方案爲您的問題:

改變你的模態功能:

Modal.php

public function login($username, $password) { 
    $this->db->where('username', $username); 
    $this->db->where('md5(password)', $password); 
    $result = $this->db->get('tblLogin'); 
    $ret = $result->row(); 
    if (!empty($ret)) { 
     return $ret; 
    } else { 
     return false; 
    } 
} 

請改變你的控制器文件。 LoginController.php

public function login() { 
    $this->form_validation->set_rules('username', 'User Name', 'required'); 
    $this->form_validation->set_rules('password', 'Password', 'required'); 
    if ($this->form_validation->run() === FALSE) { 
     $this->load->view('login'); 
    } else { 
    //get user name 
    $username = $this->input->post('username'); 
    //get encrypt password 
    $password = $this->input->post('password'); 

    //login user 
    $user_id = $this->auth_model->login($username, $password); 
    if ($user_id) { 
     //create session 
     $user_data = array(
      'user_id' => $user_id, 
      'username' => $username, 
      'logged_in' => true 
     ); 
     $this->session->set_userdata($user_data); 
     //set message 
     $this->session->set_flashdata("success", "your are logged in"); 
     redirect('index.php/users/profile'); 
     } else { 
      $this->session->set_flashdata("error", "No such account exists in database"); 
      redirect('index.php/auth/login'); 
     } 
    } 
} 
+0

不工作相同的錯誤..如果我刪除md5它的執行 –