UserName - 密码检查功能返回“false”

时间:2016-10-11 08:46:30

标签: php codeigniter codeigniter-3

我在管理网站登录时遇到了一些麻烦。我尝试了几种不同的方法和查询,这些方法和查询似乎都导致了同样的问题:我用来检查usernamepassword的方法总是返回false

为了更容易理解,这是我正在尝试做的事情:

  • 控制器文件调用视图页面并提示用户usernamepassword
  • 我正在使用callback函数为表单
  • 设置规则
  • callback函数调用模型方法
  • 如果登录详细信息正确,则模型方法返回TRUE,否则返回FALSE

显然它总是返回FALSE,因为登录永远不会有效......

我正在使用CodeIgniter。

控制器文件:

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>

抱歉英语不好,谢谢你的帮助。

1 个答案:

答案 0 :(得分: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;
        }
    }
}
相关问题