-1

このタイプのすべての質問を確認しましたが、どれもうまくいきません。ここで助けが得られることを願っています。index という名前のコントローラーと Index_model という名前のモデルがあります。インデックスコントローラーにユーザーログインとサインアップ関数を記述し、モデルでデータ関連のクエリを実行しましたが、codeigniter コールバック関数を除いてすべて正常に動作しています。

false を返すたびにコールバックを使用してユーザー ログインの詳細を検証しようとすると、コールバックを削除すると正常に動作します。私はどこが間違っているのかわかりません。私のコントローラーとモデルについては、以下のコードを見つけてください。前もって感謝します。

コントローラ:

class Index extends CI_Controller{

    public function __construct(){
        parent:: __construct();
        $this->load->helper('url');
        $this->load->model('index_model');
    }

   public function signin(){
        $this->load->helper('form');
        $data['title'] = "Sign In";
        $data['headline'] = "Sign In";
        $data['introduction'] = "Sign In to manage your business, clients and more!";
        $data['include'] = "user/signin";
        $this->load->view('template/template', $data);
    }

    public function login(){
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'required|callback_validate_login');
        $this->form_validation->set_rules('password', 'Password', 'required|md5');

        if($this->form_validation->run() == TRUE){
            $data['msg'] = "User Login Successfully!!";
            $this->load->view('user/home', $data);
        }else{
            $this->signin();
        }

    }

    public function validate_login(){
        $this->load->library('form_validation');

        $result = $this->index_model->validate_user();
        if($result == TRUE){
            return TRUE;
        }else{
            $this->form_validation->set_message('validate_login', 'Incorrect username / password.');
            return FALSE;
        }
    }
}

モデル:

class Index_model extends CI_Model{
    public function __construct(){
        $this->load->database();
    }

    /******User Login*************/
    public function validate_user(){
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('user');

        if($query->num_rows() == 1){
            return TRUE;
        }else{
            return FALSE;
        }
    }
}

私のコードを確認してください。非常に役立ちます。

4

3 に答える 3

0

あなたが使用する必要があります

$this->db->where('password', md5($password)); 
/* you should use md5($password) instead of just $password since you are using md5 as hashing technique */

あなたが使用した

$this->form_validation->set_rules('password', 'Password', 'required|md5');

パスワードはmd5、コールバックプロセスではなく、検証の実行後に変換されます

$this->form_validation->run() //i.e. after this

注 :現在、md5 はまったく安全ではありません。虹のテーブル攻撃に注意してください。ハッシュ手法を変更した方がよいでしょう。SO でのパスワードのハッシュ化とセキュリティに関しては、多くの質問があります。

于 2014-10-14T12:36:22.003 に答える