0

当我在 Codeigniter 中提交登录表单时,我一直收到“未定义”,我真的找不到问题,我尝试设置加密密钥,即使我没有使用它,但这并不能解决我们的问题

这是我的看法

<!--loads the header-->
<?php $this->load->view('abt-header');?>
<!--this is the login page-->

<div data-role="page" id="Abt-login" data-add-back-btn="true">
    <div data-role="header" data-position="fixed">
        <h1>Login</h1>

    </div>
    <div data-role="content">

        <?php echo form_open('index.php/abovetheblues/login'); ?>
        <div data-role="fieldcontain">
            <p><?php echo validation_errors(); ?></p>

            <p>
                <label>Username: </label>
                <?php echo form_input('username', set_value('username') ); ?>

            </p>
            <p>
                <label>Password: </label>
                <?php echo form_password('password', set_value('password') ); ?>

            </p>

            <p>
                <?php echo form_submit('submit', 'Submit'); ?>
            </p>  

        </div> 
        <?php echo form_close(); ?>
        </div>

</div>

这是我在模型中的功能

// checks username and password against the database
        function check_login() {
    $this->load->database();

    $this->db->where('email', $this->input->post('email'));
    $this->db->where('password', $this->input->post('password'));
    $q = $this->db->get('user');

    if ($q->num_rows() == 1) {
        return true;
    }
}

这是我在控制器中的代码

 public function login() {

    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]|max_length[12]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|max_length[6]|xss_clean');

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

        // return main page if submitted form is invalid.

        $this->load->view('abt_login');

    } else {

         $this->load->model('abt_db');
    $q = $this->abt_db->check_login();


    if($q)
    {
        $data = array(
            'email'         => $this->input->post('email'),
            'password'      => $this->input->post('password'),
        );

        $this->session->set_userdata($data);
        redirect('index.php/abovetheblues/abt-abovetheblues');
    }
    else
    {
       redirect('index.php/abovetheblues/abt_selfhelp');
    }


    }
}
4

1 回答 1

0

修改这一行:

$user_id = $this->abt_db->$this->check_login($username, $password);

到这条线

$user_id = $this->abt_db->check_login($username, $password);

编辑

此外,您的查询看起来会更好,如下所示:

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

if($q->num_rows == 1)
        {
            return true;
        }

编辑 2

这就是我一直这样做的方式:

模型:

function validate()
    {
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', sha1($this->input->post('password')));
        $q = $this->db->get('users');

        if($q->num_rows == 1)
        {
            return true;
        }
    }

控制器:

function validate_credentials()
    {
        $this->load->model('site_m');
        $q = $this->site_m->validate();


        if($q)
        {
            $data = array(
                'email'         => $this->input->post('email'),
                'is_logged_in'  => true
            );

            $this->session->set_userdata($data);
            redirect('admin/dashboard');
        }
        else
        {
            $this->message->set('failure','Wrong email or password!');
            $this->index();
        }
    }

它一直有效。我认为您在控制器中也缺少一些东西。

于 2013-08-06T14:21:19.643 回答