2012-11-19 5 views
0

사용자 로그인입니다. 따라서 사용자가 전자 메일과 암호를 입력 한 후에 해당 전자 메일과 암호를 가진 사용자가 있는지 여부를 확인하려고합니다. 이런 이유로 나는 where 절을 두 번 사용하지만 어떻게 든 작동하지 않습니다. 나는 그것을 잘못 사용하고 있기 때문에 아마도. 내 아래 코드를 잘 작동codeigniter 문제에서 where 절을 사용합니다.

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

'$ pass '를'md5'(또는 뭔가)하고 싶다고 가정합니다. $ 소금'? –

+1

이 코드는 문제가 해결되지 않았습니까? 쿼리를 실행 한 후,'$ this-> db-> last_query()'를 echo하여 어떤 SQL CodeIgniter가 실행 중인지 확인하십시오. –

+0

그냥 ** $ pass 사이의 공백을 제거하십시오. $ salt **는이 코드에서 내가 싫어하는 유일한 것입니다. – sbaaaang

답변

0

수정 수있는 방법, 그 잘못이고, 단지 그들을 연결, 암호 + 소금을 통과하고 있지만, 당신은 아무것도를 인코딩하지 않는 두 번째입니다. 따라서 암호가 bob이고 salt가 후추이면 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) 
} 
관련 문제