php - 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');
}
?>
谁能帮我解决这个问题?
解决方案
请通过传递如下数组来尝试消息。
$this->load->library('session');
$this->session->set_flashdata('err_message', array('message' => 'Username Not Entered','class' => 'alert alert-warning'));
谢谢。
推荐阅读
- javascript - 单击按钮后引导模式不起作用
- javascript - 如何使用扩展运算符在 javascript 中的对象数组中删除和添加新键
- image - 如何在颤动中将图像存储到缓存的网络图像
- javascript - 使用 webpack 5 将主题提取到不同的 css
- java - 将 for 循环转换为流
- raspberry-pi - 树莓失去WiFi连接,无法重新连接
- reactjs - 使用检查的材料 UI 自动完成组件问题
- php - laravel 门不能在带有 @can 的刀片中工作
- reactjs - 尝试创建 React 应用程序并面临错误
- shared-libraries - 即使在创建过程中链接了可执行文件,它是否还需要这样的文件?