【发布时间】:2016-05-15 10:51:10
【问题描述】:
这是我的代码,我正在尝试通过数据库进行管理员访问...但似乎我的用户类型验证没有得到任何东西。我试图 print_r 它并且输出只是 array(),你认为我的错误是什么。这是我的代码..
登录控制器
public function loginValidate(){
$this->load->library('form_validation');
$this->load->model('login_model');
$this->form_validation->set_rules('USERNAME','Username','required|trim|callback_validateCreds');
$this->form_validation->set_rules('PASSWORD','Password','required|trim|md5');
if ($this->form_validation->run() == FALSE){
$this->index();
}else{
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'is_logged_in' => 1,
'value' => $this->login_model->returnQuery()
);
$this->session->set_userdata($data);
redirect('login/adminIndex');
$q = $this->login_model->returnQuery();
if ($q = '1'){
redirect('login/adminIndex');
}elseif ($q = '2') {
redirect('login/userIndex');
}else{
redirect('login/restricted');
}
}
}
public function validateCreds(){
$this->load->library('form_validation');
$this->load->model('login_model');
if($this->login_model->can_log_in()){ // can_log_in() model
return true;
}else{
$this->form_validation->set_message('validateCreds','Username and password did not match.'); //return message to validateCreds
return false;
}
}
这是我的模型
public function can_log_in(){
$sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
return $query;
if ($query->num_rows() == 1){
return true;
}else{
return false;
}
}
public function returnQuery(){
$sql = "SELECT USERCODE FROM tbl_users where USERNAME =? AND PASSWORD = ?";
$data = array(
'USERNAME' => $this->input->post('USERNAME'),
'PASSWORD' => $this->input->post('PASSWORD')
);
$query = $this->db->query($sql, $data);
return $query->result();
}
没有任何错误但返回true给
if ($q = '1'){
redirect('login/adminIndex');
即使它不是 = 1.. 我也尝试过 == 并取消 '' 但它只会将我引导到受限页面。
【问题讨论】:
-
=不是if中的比较运算符,而是赋值运算符 -
我将其更改为 == ,它只是返回 true 为 ` if ($q == '1'){ redirect('login/adminIndex');` 我正在尝试访问其他用户这是 == '2' ...
-
而
print_r($this->login_model->returnQuery());的输出仍然只是array() -
您无需在模型中发布。您可以简单地从 $this->input->post() 读取数据,这也将提供该模型中的详细信息。或者您可以使用 $_POST 分配可以在另一个模型中读取的新值
-
redirect('login/adminIndex');将清除您所有的帖子数据。重定向之前的更新也是如此
标签: php sql codeigniter admin