2011-11-15 3 views
0

他のユーザーのパスワードでもログインできます。私は何が間違っているのか理解できません。このCodeIgniterログインスクリプトの重大な問題

function login() 
{ 
    $this->form_validation->set_rules('username','Username','trim|xss_clean|required|callback_login_user_check'); 
    $this->form_validation->set_rules('password','Password','trim|xss_clean|required|min_length[4]|max_length[20]|callback_password_check|sha1'); 

    $this->_username = $this->input->post('username'); 
    $this->_password = $this->_salt.sha1($this->input->post('password')); 


    if($this->form_validation->run() == FALSE) 
    { 
     $toView['title']= 'Please login'; 
     $this->build_content($toView); 
     $this->render_page(); 
    } 
    else 
    { 

     $this->account_model->login(); 


     //$data['message'] = "You are logged in! Now go to ". anchor("members/dashboard","Dashboard"); 
     redirect('members/dashboard'); 

    } 

} 



//--------------------------------------------------------------------------------------------------------------------------- 

// password CHECKING 

function password_check(){ 
    $this->db->where('username', $this->_username); 
    $this->db->where('password', $this->_password); 
    $query = $this->db->get('users'); 
    $result = $query->row_array(); 

    if ($query->num_rows() == 0) 
    { 
     $this->form_validation->set_message('password_check','There was an error! '); 
     return FALSE; 
    } 

    if($result['password'] == $this->_password) 
    { 
     return TRUE; 
    } 

} 





function login_user_check($user) 
{ 
    $query = $this->db->get_where('users', array('username'=>$user)); 

    if(!$query->num_rows()>0) 
    { 
     $this->form_validation->set_message('login_user_check', 'The %s does not exists in our database'); 
     return FALSE; 
    } 

    if($query->num_rows() > 0) 
    { 
     foreach($query->result_array() as $row) 
     { 
      //$data[$row['id']] = $row['name']; 
      $this->session->set_userdata('user_id', $row['user_id']); 
     } 
    } 

    $query->free_result(); 
    return true; 
} 
+2

あなたが正確に何だと思いますが達成されていますあなたは 'where'を2回連続で呼んでいますか? –

+0

私は専門家ではありませんが、2つの$ this-> db-> ORまたはANDで句を組み合わせるのですか? – Dennis

+0

[documentation](http://codeigniter.com/user_guide/database/active_record.html#where): '$ this-> db-> where( 'name'、$ name); $ this-> db-> where( 'title'、$ title); $ this-> db->ここで( 'status'、$ status); // WHERE name = 'Joe' AND title = 'boss' AND status = 'active''私の人生を保存するためにデータベースに問い合わせる難しい方法を考え出すことができません:P – Esailija

答えて

-2

パスワードが等しくない場合、あなたはfalseを返さない。次のように試してみてください。

function password_check(){ 
    $this->db->where('username', $this->_username); 
    $this->db->where('password', $this->_password); 
    $query = $this->db->get('users'); 
    $result = $query->row_array(); 

    if ($query->num_rows() == 0) 
    { 
     $this->form_validation->set_message('password_check','There was an error! '); 
     return FALSE; 
    } 

    return TRUE; 

} 

EDITED:return TRUEで十分です。コメントを参照してください。

+0

パスワードが一致しているかどうかをチェックするのは、その時点で単純に 'TRUE'を返すことができます。問題は他にもある。 – minboost

+0

私の悪いです。あなたは正しいです、申し訳ありません! – Dennis

0

私はコードをテストし、それは私のために働く。他のユーザーのパスワードでログインすることはできません。あなたのaccount_model->login()が何をしているのか分かりませんが、この時点であなたのスクリプトは正常に動作しているようです。

、あなたは(password_check内部()関数)を置き換えることができます

return TRUE; 

if($result['password'] == $this->_password) 
{ 
    return TRUE; 
} 

(前述したように)

0
function password_check(){ 

    // Check if one of values is empty, return false 
    if (empty($this->_username)||empty($this->_password)) return false; 

    $this->db->where('username', $this->_username); 
    $this->db->where('password', $this->_password); 

    ... 
}