[英]Codeigniter admin and user role
I'm making a login system with 2 roles one for 'admin' and one for 'user'.我正在制作一个有 2 个角色的登录系统,一个是“管理员”,一个是“用户”。 I tried to make a else if statement for the login.
我试图为登录做一个 else if 语句。 This for the user login actually what happens now that every user is going to the admin_view right now.
这对于用户登录实际上发生了什么,因为每个用户现在都要去 admin_view。
In my database the row is called 'admin' which is a boolean so if the column 'admin' is 1 in the users table.在我的数据库中,该行名为“admin”,它是一个布尔值,因此如果用户表中的“admin”列为 1。 The admin_view should be loaded if the value is 0 the user_view needs to be loaded.
如果值为 0,则应加载 admin_view 需要加载 user_view。
I really don't know what to do now.我现在真的不知道该怎么办。
How do I need to seperate the roles between the admin and user?我需要如何分离管理员和用户之间的角色?
Here is my controller: users.php这是我的控制器:users.php
public function login() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[3]');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[3]|matches[password]');
if ($this->form_validation->run() == FALSE) {
$data = array(
'errors' => validation_errors()
);
$this->session->set_flashdata($data);
redirect('home');
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$user_id = $this->user_model->login_user($username, $password);
if($user_id) {
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
'admin' => 1;
);
$this->session->set_userdata($user_data);
$this->session->set_flashdata('login_success', 'You are now logged in');
$data['main_view'] = "admin_view";
$this->load->view('layouts/main', $data);
}
// Here I want the user_view to load op if the user haa a 0 value in
in the column admin.
else if($user_id) {
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true,
'admin' => 0;
);
$this->session->set_userdata($user_data);
$this->session->set_flashdata('login_success', 'You are now logged in');
$data['main_view'] = "user_view";
$this->load->view('layouts/main', $data);
} else {
$this->session->set_flashdata('login_failed', 'Sorry, you are not logged in');
redirect('home/index');
}
}
}
Here is my model: user_model.php这是我的模型:user_model.php
public function login_user($username, $password) {
$this->db->where('username', $username);
$result = $this->db->get('users');
$db_password = $result->row(2)->password;
$admin = $result->row(7)->admin;
if(password_verify($password, $db_password)) {
return $result->row(0)->id;
} else {
return false;
}
}
}
I apperciate the help.我感谢帮助。
First off, there is a typo in your array, the last one should not end with a ;
首先,您的数组中有一个错字,最后一个不应以
;
结尾;
. .
In your user_model
, instead of returning the ID, return some more information.在您的
user_model
,不是返回 ID,而是返回更多信息。
eg:例如:
return $result->row(0);
Then, when you go back to your controller, you can do然后,当你回到你的控制器时,你可以做
$data = $this->user_model->login_user($username, $password);
array(
'user_id' => $data->id,
'username' => $username,
'logged_in' => true,
'admin' => $data->admin
);
From there, you could check in the session data whether admin
is 0
or 1
.从那里,您可以检查会话数据
admin
是0
还是1
。
Then you can do :然后你可以这样做:
if ($admin == 0) {
//send to wherever
} else {
//send some place else
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.