首页 > 解决方案 > codeigniter中的登录错误消息验证

问题描述

如果用户没有输入用户名和密码并尝试单击登录,我将尝试显示错误消息。我正在尝试显示错误消息,例如未输入用户名和未输入密码。但它不能完美地工作。

这是我的控制器:

public function login_user() {

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

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

        $this->load->view('login_view');
    } else {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password'),
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname')
        );
        $result = $this->login_database->login($data);
        if ($result == TRUE) {
                    // Add user data in session
            $this->session->set_userdata('username', $data['username']);
            $this->session->set_userdata('firstname', $data['firstname']);
            $this->session->set_userdata('lastname', $data['lastname']);

            //redirect to dashboard
            $this->load->view('include/sidenavbar');
            $this->load->view('include/topnavbar');
            $this->load->view('dashboard');
        } else {
            if($data['username']=='')
            {
               $this->session->set_flashdata('err_message', 'Username Not Entered !');
            }

            $this->session->set_flashdata('err_message', 'Login is invalid. Please try again !');
            $this->load->view('login_view', $data);
        }
    }
}

这是我的模型:

public function login($data) {

    $condition = "username =" . "'" . $data['username'] . "' AND " . "password =" . "'" . $data['password'] . "'";
    $this->db->select('*');
    $this->db->from('users');
    $this->db->where($condition);
    $this->db->limit(1);
    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return true;
    } else {
        return false;
    }
}

看法:

<?php 
if( $this->session->flashdata('err_message') )
{
    echo $this->session->flashdata('err_message');
}
?>

谁能帮我解决这个问题?

标签: phpcodeigniter

解决方案


请通过传递如下数组来尝试消息。

$this->load->library('session');

$this->session->set_flashdata('err_message', array('message' => 'Username Not Entered','class' => 'alert alert-warning'));

谢谢。


推荐阅读