2016-10-11 32 views
1

我在管理我的網站登錄時遇到了一些麻煩。我嘗試了幾種不同的方法和查詢,這些方法和查詢似乎都導致了同樣的問題:我用來檢查usernamepassword的方法總是返回false用戶名 - 密碼檢查功能返回「false」

爲了便於理解,這裏就是我想要做的事:

  • 控制器文件調用瀏覽頁面,並提示用戶爲usernamepassword
  • 我使用callback功能設置對於形式
  • callback功能的規則調用模型方法
  • 模型方法返回TRUE如果登錄信息是否正確,否則爲0。

顯然,它總是返回FALSE作爲登錄永遠不會奏效......

我用笨。

Controller文件:

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; 
    } 
    } 
} 

型號文件:

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; 
    } 
    } 
} 

用處不大,這裏的登錄視圖頁面的一部分:

<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> 

對不起,我的英語不好,並感謝您幫幫我。

+0

在存儲之前,您沒有對用戶密碼進行哈希處理嗎? –

+0

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

+0

爲什麼你使用像'callback_login_check [password]這樣的回調? – Shihas

回答

2

我已經改變了一些東西放在這裏,您可以嘗試

我希望你正在使用的密碼好的哈希NOT MD5使用http://php.net/manual/en/function.password-hash.phphttp://php.net/manual/en/function.password-verify.php

文件名: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

只是,你能向我解釋爲什麼用第二個參數使用回調函數不起作用嗎? –