在 CI 中,我想登录我的数据库 namd 的 3 个不同数据库表中存在的 3 个不同的 usr 角色计数为 m_admin,m_faculty,m_supervisior



在代码点火器中,我想从一个登录页面登录 3 个不同角色的不同用户,这些用户的用户名和密码在我的控制器中使用"if elseif &else"显示在不同的数据库表中,我认为如果表单验证为真,那么它将转到 if 和 if 不正确时,它将转到 elseif,如果它不起作用,那么它将转到第三个,即m_faculty,如果它也不起作用,那么它将显示警报,表明您为此输入了错误的 ID 和密码,否则我能够重定向到"教职员工/m_dashboard",只有这是我的教职员工仪表板,其他重定向在知道为什么它不起作用或任何更好的方法时都不起作用..........?????

public function Auth()
{
$rules = array(
array('field' => 'username', 'label' => 'Username', 'rules' => 'required'),
array('field' => 'password', 'label' => 'Password', 'rules' => 'required'),
);
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == false) {
$Msg = array('Msg' => validation_errors(), 'Type' => 'danger');
$this->session->set_flashdata($Msg);
redirect(base_url() . 'Admin/');
}
# faculty dashboard dashboard

elseif ($this->form_validation->run() == true) 
{
$username = $this->lib->validate($this->input->post('username'));
$password = $this->lib->validate($this->input->post('password'));
$username = $this->security->xss_clean($username);
$password = $this->security->xss_clean($password);          
$count = $this->lib_model->Counter('m_faculty', array('username' => $username, 'password' => $password, 'status' => 0));
if ($count == 1) {
$Sessions = array(
'EmpCode' => $username,
'EmpName' => 'Administration',
'ELogin' => true);
$this->session->set_userdata($Sessions);
$Msg = array('Msg' => ' Have a Nice Day ', 'Type' => 'success');
$this->session->set_flashdata($Msg);
redirect(base_url() . 'Faculty/f_dashboard');

}
}
#supervisior dashboard
elseif ($this->form_validation->run() == true) 
{    
$username = $this->lib->validate($this->input->post('username'));
$password = $this->lib->validate($this->input->post('password'));
$username = $this->security->xss_clean($username);
$password = $this->security->xss_clean($password);          
$count = $this->lib_model->Counter('m_supervisior', array('username' => $username, 'password' => $password, 'status' => 0));            
if ($count == 1) {
$Sessions = array(
'EmpCode' => $username,
'EmpName' => 'Administration',
'ELogin' => true);
$this->session->set_userdata($Sessions);
$Msg = array('Msg' => ' Have a Nice Day ', 'Type' => 'success');
$this->session->set_flashdata($Msg);
redirect(base_url() . 'Supervisior/supervisior_dashboard');
}
}
#admin dashboard

elseif ($this->form_validation->run() == true)  
{
$username = $this->lib->validate($this->input->post('username'));
$password = $this->lib->validate($this->input->post('password'));
$username = $this->security->xss_clean($username);
$password = $this->security->xss_clean($password);          
$count = $this->lib_model->Counter('m_admin', array('username' => $username, 'password' => $password, 'status' => 0));          
if ($count == 1) {
$Sessions = array(
'EmpCode' => $username,
'EmpName' => 'Administration',
'ELogin' => true);
$this->session->set_userdata($Sessions);
$Msg = array('Msg' => ' Have a Nice Day ', 'Type' => 'success');
$this->session->set_flashdata($Msg);
redirect(base_url() . 'Admin/dashboard');
} 
}
#error msg
else {
$Msg = array('Msg' => 'Username and Password Invalid', 'Type' => 'danger');
$this->session->set_flashdata($Msg);
redirect(base_url() . 'Admin/');
}
}

你的代码将无法工作,因为在第一次验证检查之后,它总是会卡在你的第一个else子句中。

我不确定你到底在这里做什么 - 但一种可能的方法是

public function Auth()
{
try
{
$rules = array(
array('field' => 'username', 'label' => 'Username', 'rules' => 'required'),
array('field' => 'password', 'label' => 'Password', 'rules' => 'required'),
);
$this->form_validation->set_rules($rules);
if (!$this->form_validation->run()) throw new Exception(validation_errors());
$username = $this->lib->validate($this->input->post('username'));
$password = $this->lib->validate($this->input->post('password'));
$arrAuthenticationTypes = [
[
'table' => 'm_faculty',
'view' => 'Faculty/f_dashboard'
],
[
'table' => 'm_supervisior',
'view' => 'Supervisior/supervisior_dashboard'
],
[
'table' => 'm_admin',
'view' => 'Admin/dashboard'
],
];
foreach($arrAuthenticationTypes AS $arrAuthenticationType)
{
$count = $this->lib_model->Counter($arrAuthenticationType['table'], array('username' => $username, 'password' => $password, 'status' => 0));
if ($count == 1)
{
$Sessions = array(
'EmpCode' => $username,
'EmpName' => 'Administration',
'ELogin' => true
);
$this->session->set_userdata($Sessions);
$Msg = array('Msg' => ' Have a Nice Day ', 'Type' => 'success');
$this->session->set_flashdata($Msg);
redirect(base_url() . $arrAuthenticationType['view']);
}
}
throw new Exception('Username and Password Invalid');
}
catch(Exception $e)
{
$arrMessage = [
'Msg' => $e->getMessage(),
'Type' => 'danger'
];
$this->session->set_flashdata($Msg);
redirect(base_url() . 'Admin/');
}
}

相关内容

  • 没有找到相关文章

最新更新