2012-11-19 67 views
0

我是用戶登錄。因此,在用戶輸入電子郵件和密碼後,我想檢查是否存在具有該電子郵件和密碼的用戶。由於這個原因,我使用where子句兩次,但不知何故它不工作。也許是因爲我錯誤地使用了它。我怎樣才能解決我下面的代碼在codeigniter問題中使用where子句

$this->db->where('email', $this->input->post('email')); 
$this->db->where('password', $pass . $salt); 
+0

我假設你想'md5'(或者其他)'$ pass。 $ salt'? –

+1

這段代碼很好,也許問題在別處?運行查詢後,嘗試回顯'$ this-> db-> last_query()'以查看SQL CodeIgniter的運行情況。 –

+0

只是刪除** $ pass之間的空格。 $鹽**是我不喜歡在這個代碼的唯一的事情 – sbaaaang

回答

0

的地方工作正常,這是第二個地方是錯的,你傳遞密碼+鹽,但你不編碼任何東西,只是串聯它們。所以如果你的密碼是bob,而你的鹽是胡椒粉,你會將bobpepper作爲密碼字段傳遞給查詢。

根據不同的加密代碼應該類似於此:

$password = md5($this->input->post('password'). $salt); //swap the md5 for whatever encryption method you're using. 
$this->db->where('email', $this->input->post('email')); 
$this->db->where('password', $password); 
0

你不需要做你的where子句兩次。

public function login(){ 

    try 
    { 
     $user = $this->db->select()->where()->get(); // mysql query 

     if(!$user) 
      throw new Exception("User not found!"); 

     if(!$this->compare_password($this->input->post('password'), $user->password)) 
      throw new Exception("Passwords did not match!"); 
    } 
    catch(Exception $e) 
    { 
     $this->session->set_flashdata('error', $e->getMessage()); 
     log_message('error', $e->getMessage()); 
     redirect('/'); 
    } 

    //we got this far so everything should be OK 

    $this->session->set_flashdata('success', 'Login Successfull'); 
    redirect('dashboard'); 
} 

注:您可能需要使用更好的密碼散列algorithim比這個生產模式!

protected function hash_password($password){ 
    return sha1($pw . $this->config->item('encryption_key')); 
} 

protected function compare_password($pw, $dbpw){ 
    return (sha1($pw . $this->config->item('encryption_key')) === $dbpw) 
}