用户验证在CodeIgniter中不起作用

时间:2017-06-15 07:58:04

标签: php codeigniter validation

我是codeigniter的新手。我创建一个登录表单,我想验证我的登录凭据。我搜索了许多解决方案但我失败了。我无法验证我的凭据。

以下是我的观点代码名称为login_form.php

<div id="login_form">
        <h1>Login Page..!!</h1>
        <?php
        echo form_open('login/validate_credentials');
        echo form_input('username', 'Username');
        echo form_password('password', 'Password');
        echo form_submit ('submit', 'Login');
        ?>

    </div>

以下是我的型号代码名称为users_model.php

<?php
class Users_model extends CI_Model{

        function validate(){

                $this->db->where('username', $this->input->post('username'));
                $this->db->where('password', $this->input->post('password'));
                $query = $this->db->get('users');
if ($query->num_rows == 1){
                    return true;
                    }

            }

    } ?>

以下是我的控制器代码名称Login.php

<?php

    class Login extends CI_Controller{

        function index(){
            $this->load->helper('url'); 
            $this->load->helper('html');
            $data['main_content'] = 'login_form';
            $this->load->view('include/template', $data);
        }

        function validate_credentials(){

            $this->load->model('users_model');
            $query = $this->users_model->validate();




            if($query){


                $data = array(
                                'username'=> $this->input->post('username'),
                                'is_logged_in' => true
                );

                    $this->session->set_userdata($data);
                    redirect('site/dashboard');

            }

            else{
                echo "Wrong";
                //$this->index();   
            }
        }
    }
?>

即使我给它正确的用户名和密码,也无法验证并回复“错误”,如我的代码所示

1 个答案:

答案 0 :(得分:1)

num_rows()应该是一种方法

改变它:

  function validate()
  {

            $this->db->where('username', $this->input->post('username'));
            $this->db->where('password', $this->input->post('password'));
            $query = $this->db->get('users');
            if ($query->num_rows() == 1){
                return true;
                }

  }

https://www.codeigniter.com/user_guide/database/results.html#id4