2014-01-30 152 views
0

我有以下的類來驗證用戶登錄,笨NUM_ROWS()總是返回0

class Validation_model extends CI_Model { 


public function validateLogin($userid,$userpass){  


    $this->load->library('encrypt'); 
    $this->load->database(); 
    $encrypted_string = $this->encrypt->encode($userpass); 

    $this->db->select('*'); 
    $this->db->from('ecom-user'); 
    $this->db->where('userid', $userid); 
    $this->db->where('userpassword', $encrypted_string); 

    $result = $this->db->get(); 


    echo $result->num_rows(); 




    } 


} 

但當連我進入了一個有效的用戶ID &密碼,它返回的行數爲0,請幫我解決這個問題,我犯了什麼錯誤?

+1

打印最後一次查詢,並在您的phpmyadmin運行 –

+0

它也返回零:( –

+0

大概$ encrypted_string不工作...嘗試在查詢中手動輸入密碼,看看它是否在 – Makrand

回答

1

它不起作用,因爲你是加密的密碼。

CodeIgniter使用的加密函數每次加密時都會使用隨機初始化向量(IV),這意味着密文(加密文本)在每次加密時都會有所不同。

當處理密碼時你需要hash他們不是加密他們。他們是相關的,但他們不是一回事。

如果你有PHP> = 5.5.0,那麼你可以這樣做:

$hashed_password = password_hash($userpass, PASSWORD_DEFAULT); 
// ... 
$this->db->where('userpassword', $hashed_password); 

爲了達到這個目的,您還需要重新對數據庫中的密碼進行散列。

+0

非常感謝,我明白了! –