2015-06-01 25 views
3

我有一個用戶的表,其中密碼列的數據使用Codeigniter中的加密庫進行編碼。現在,我想選擇編碼列進行解碼並與密碼的用戶輸入(登錄驗證)進行比較。這是代碼。 我插入這樣的值:如何選擇和解碼codeigniter查詢中的列值

$this->db->insert("my_table",Array("username"=>$this->input->post("username"),"password"=>$this->encrypt->encode($this->input->post("password")))); 

現在我驗證輸入這樣:

$data = $this->db->get("mytable"); 
    foreach($data as $d){ 
    if($d["username"] == $this->input->post("username") && $d["password"] == $this->encrypt->decode($this->input->post("password")){ 
    //success 
    break; 
    } 
} 

這工作那麼好給我,但我希望有一個更短和更清潔的方式來做到這一點。你也知道,未來的編碼實踐。 以下是我迄今爲止所做的:

$this->db->get_where("my_table",Array($this->encrypt->decode("password")=>$this->input->post("password"))); 

但是啊!這會返回一條錯誤消息。錯誤說:

Unknown column '0' on where clause 
+0

而且什麼是你得到的錯誤。發佈該錯誤太.. –

+0

剛更新的問題 – Kentot

回答

0

問題是,您將解碼的密碼設置爲數據庫列。

Array($this->encrypt->decode("password")=>$this->input->post("password")) 

你需要做什麼更多的東西一樣:

Array("password" => $this->encrypt->decode("password")) 
0

首先,是不是如果可以解密加密密碼是個好主意!

和最佳實踐是加密輸入,然後在數據庫中選擇

$this->db->select('*') 
$yhis->db->from('mytable') 
$this->db->where('username', $this->input->post("username")); 
$this->db->where('password', $this->encrypt->encode($this->input->post("password"))); 
$this->db->get(); 

如果結果......你登錄