2016-10-11 3 views
1

ウェブサイトのログイン管理に問題があります。私は同じ問題につながるいくつかの異なるメソッドとクエリを試しました。私がusernamepasswordをチェックするメソッドは、常にfalseを返します。UserName - 「false」を返すパスワードチェック機能

それが簡単に理解できるようにするため、ここで私がやろうとしているものです:

  • コントローラファイルの呼び出しは、ページを表示し、私が設定しcallback機能を使用していusernamepassword
  • をユーザに促しログイン情報が正しいと場合callback機能はモデル法
  • モデルのメソッドを呼び出す形
  • のためのルールがTRUEを返します。そうでなければ0。ログインは決して働かないよう

は、どうやらそれは常に私はCodeIgniterのを使用してい

... FALSEを返します。

コントローラファイル:

class Login extends CI_Controller { 

    public function __construct() 
    { 
    parent::__construct(); 
    $this->load->helper(array('form', 'url')); 
    $this->load->library(array('form_validation', 'session')); 
    } 

    public function index() 
    { 
    $this->form_validation->set_rules('username', 'Username', 'callback_login_check[password]'); 

    if ($this->form_validation->run() == FALSE) { 
     //loading view as long as it is not correct 
     $this->load->view('login'); 
    } else { 
     //temporary successful login page 
     $this->load->view('success'); 
    } 
    } 

    public function login_check($usr, $pwd) 
    { 
    $this->load->database(); 
    $this->load->model('EZ_Query'); 

    $result = $this->EZ_Query->get_user_details($usr, $pwd); 

    if ($result == TRUE) { 
     return TRUE; 
    } else { 
     $this->form_validation->set_message('login_check', 'Password does not match Username'); 
     return FALSE; 
    } 
    } 
} 

モデルファイル:

の悪い英語のため申し訳ありません
<body> 
    <?php 
     echo validation_errors(); 
     echo form_open('Login'); 
     echo form_label('Username', 'username'); 
     echo form_input('username')."<br />"; 
     echo form_label('Password', 'password'); 
     echo form_password('password')."<br />"; 
     echo form_submit('sumbit', 'Enter'); 
     echo form_close(); 
    ?> 
    </body> 

、そしてあなたのための感謝:

class EZ_Query extends CI_Model { 

    public function get_user_details($usr, $pwd) 
    { 
    $sql = "SELECT * 
       FROM PROFIL 
       WHERE USER = '$usr' 
       AND MDP = '$pwd'"; 

    $query = $this->db->query($sql); 

    if ($query->num_rows() == 1) { 
     $row = $query->row(); 

     //session variables 
     $data = array('name' => $row->NOMPROFIL, 
        'fname' => $row->PRENOMPROFIL, 
        'type' => $row->TYPEPROFIL); 
     $this->session->set_userdata($data); 

      return TRUE; 
    } else { 
     return FALSE; 
    } 
    } 
} 

あまり有用で、ここでのログインビューページの一部です助けて。あなたは私はあなたが良いパスワードのハッシュませMD5使用http://php.net/manual/en/function.password-hash.phphttp://php.net/manual/en/function.password-verify.php

を使用している願っています

をしようとするため

+0

保存する前にユーザーのパスワードをハッシュしませんか? –

+0

http://www.codeigniter.com/user_guide/general/styleguide.html#file-naming – user4419336

+0

なぜ 'callback_login_check [password]'のようなコールバックを使用しますか? – Shihas

答えて

2

は、私はここで物事のカップルを変更したファイル名:Login.php

<?php 

class Login extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
     $this->load->library(array('form_validation', 'session')); 
     // Autoload the session because you may need it else where 
    } 

    public function index() { 
     // remove the word password from callback as it on the username 
     // login all ways good to use required 

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

     if ($this->form_validation->run() == FALSE) { 

      //loading view as long as it is not correct 
      $this->load->view('login'); 

     } else { 

      //temporary successful login page 
      $this->load->view('success'); 
     } 
    } 

    public function login_check() 
    { 
     $usr = $this->input->post('username'); 
     $pwd = $this->input->post('password'); 

     $this->load->database(); // Autoload database best. 

     // Filename of model should be Ez_query.php and same with class only first letter upper case 

     $this->load->model('ez_query'); 

     $result = $this->ez_query->get_user_details($usr, $pwd); 

     if ($result == TRUE) { 
      return TRUE; 
     } else { 
      $this->form_validation->set_message('login_check', 'Password does not match Username'); 
      return FALSE; 
     } 
    } 
} 
+0

アヘン・オン・ザ・ハッシング・ヴォルフガング。私はここで多くのログインコントローラを参照してくださいそれを使用しないでください – Brad

+0

あなたの助けをありがとう、それは素晴らしい作品!パスワードの暗号化にsha1を使用しています。 –

+0

ちょっと、2番目のパラメータでコールバック関数を使うのがなぜうまくいかないのか私に説明できますか? –

関連する問題