您好,所以我在CodeIgniter中构建了一个登录系统,在该系统中/应该允许数据库执行3个验证步骤,然后才允许访问特定页面。
三个步骤值是:active
,is_member
和is_admin
这是我在Users控制器中编写的代码:
public function login(){
// Prohibit access if already logged in
$this->User_model->session_comprobate_member();
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]');
if ($this->form_validation->run() == FALSE){
//Load View Into Template
$this->template->load('public','login','users/login');
} else {
// Get Post Data from Database
$username = $this->input->post('username');
$password = $this->input->post('password');
$enc_password = md5($password);
$data_user = $this->User_model->login($username, $enc_password);
if($data_user == true){
$user_id = $this->User_model->get_userid($username);
$users = $this->User_model->get_username($user_id);
if($users->active == 0){
// Create error
$this->session->set_flashdata('error', 'This account is banned or inactive');
// Redirect to page
redirect('dashboard/login');
}
if($users->is_admin == 0){
// Create error
$this->session->set_flashdata('error', 'You do not have permission to view this page');
// Redirect to page
redirect('dashboard/login');
}
if($users->is_member == 0){
// Create error
$this->session->set_flashdata('error', 'This account does not exists. Please try again.');
// Redirect to page
redirect('dashboard/login');
} else {
$sess_data = array(
'user_id' => $user_id,
'username' => $username,
'occupation' => 'occupation',
'is_member' => true,
'is_admin' => true,
'active' => true
);
// Set Session Data
$this->session->set_userdata($sess_data);
// Create Message
$this->session->set_flashdata('success', 'You are logged in');
// Redirect to pages
redirect('dashboard');
}
} else {
// Create Error
$this->session->set_flashdata('error', 'Invalid Login');
// Redirect to pages
redirect('dashboard/login');
}
}
}
这些值中的每一个都设置为TRUE(1)
或FALSE(0)
取决于用户帐户。我有一个树值等于1的帐户,因此它应该允许我访问;这是一张图片:登录验证满足三个值之后,即使出于某种原因,即使用户一直设置为“TRUE
我”,我仍然可以访问我想要的东西:
$this->session->set_flashdata('error', 'This account is banned or inactive');
知道如何解决吗?谢谢。
这是我的模型:
public function get($id)
{
$this->db->where('id', $id);
$query = $this->db->get($this->table);
return $query->row();
}
public function login($username, $password)
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row()->id;
} else {
return false;
}
}
//I need to work on these two
public function get_username($users) {
$this->db->select('id');
$this->db->from($this->table);
$this->db->where('username', $users);
return $this->db->get()->row;
}
public function get_userid($user_id) {
$this->db->select('id');
$this->db->from($this->table);
$this->db->where('id', $user_id);
return $this->db->get()->row();
}
///
//Check if admin
public function is_admin($id) {
$this->db->select('is_admin');
$this->db->from($this->table);
$this->db->where('id', $id);
$is_admin = $this->db->get()->row('is_admin');
if ($is_admin == 0) {
redirect('/');
} else {
redirect('admin');
}
}
//Check if member
public function is_member($id) {
$this->db->select('is_member');
$this->db->from($this->table);
$this->db->where('id', $id);
$is_member = $this->db->get()->row('is_member');
if ($is_member == 0) {
redirect('/');
} else {
redirect('dashboard/login');
}
}
//Check if active
public function is_active($id) {
$this->db->select('active');
$this->db->from($this->table);
$this->db->where('id', $id);
$is_active = $this->db->get()->row('active');
if ($is_active == 0) {
redirect('/');
} else {
redirect('dashboard/login');
}
}
再一次感谢你的帮助。
假设username
是表中的唯一列:
控制者
// user login
if($data_user == true) {
// $username from $this->input->post('username');
// call model function
$user = $this->User_model->get_username($username);
// is active user ?
if($user['active'] == 0) {
// Create error
$this->session->set_flashdata('error', 'This account is banned or inactive');
// Redirect to page
redirect('dashboard/login');
}
// is admin ?
if($user['is_admin'] == 0) {
// Create error
$this->session->set_flashdata('error', 'You do not have permission to view this page');
// Redirect to page
redirect('dashboard/login');
}
// is member ?
if($user['is_member'] == 0) {
// Create error
$this->session->set_flashdata('error', 'This account does not exists. Please try again.');
// Redirect to page
redirect('dashboard/login');
} else {
$sess_data = array(
'user_id' => $user['id'],
'username' => $user['username'],
'occupation' => 'occupation',
'is_member' => true,
'is_admin' => true,
'active' => true
);
// Set Session Data
$this->session->set_userdata($sess_data);
// Create Message
$this->session->set_flashdata('success', 'You are logged in');
// Redirect to pages
redirect('dashboard');
}
} else {
// Create Error
$this->session->set_flashdata('error', 'Invalid Login');
// Redirect to pages
redirect('dashboard/login');
}
这个模型 get_username()
public function get_username($username) {
// select field we needed
$this->db->select('id', 'username', active, is_admin, is_member);
$this->db->from($this->table);
$this->db->where('username', $username);
$this->db->limit(1);
$query = $this->db->get();
// check is $query have a data ?
if ($query->num_rows() > 0) {
// return data
return $query->row_array();
} else {
// redirect login, because no data with that username
redirect('dashboard/login');
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句