【发布时间】:2019-12-19 20:02:10
【问题描述】:
我已成功创建注册并登录。现在我想要 2 个角色,即管理员和用户。请帮助如何进行。
我的 SQL 表包含: 用户、电子邮件、密码、验证密钥、is_verified、角色(管理员和用户)。
登录控制器:
function validation()
{
$this->form_validation->set_rules('user_email', 'Email Address', 'required|trim|valid_email');
$this->form_validation->set_rules('user_password', 'Password', 'required');
if ($this->form_validation->run()) {
$result = $this->loginModel->can_login($this->input->post('user_email'), $this->input->post('user_password'));
if ($result == $query) {
redirect('Admin');
} else {
$this->session->set_flashdata('message',$result);
redirect('login');
}
} else {
$this->index();
}
}
登录模式:
function can_login($email, $password)
{
$this->db->where('email', $email);
$query = $this->db->get('codeigniter_register');
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
if($row->is_email_verified == 'yes') {
$store_password = $this->encryption->decrypt($row->password);
if($password == $store_password) {
$this->session->set_userdata('id', $row->id);
} else {
return 'Wrong Password';
}
} else {
return 'First verified your email address';
}
}
} else {
return 'Wrong Email Address';
}
}
管理控制器:
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
if(!$this->session->userdata('id')) {
redirect('login');
}
}
function index()
{
echo '<p align="center"><a href="'.base_url().'admin/logout">Logout</a></p>';*/
if($this->session->userdata('role')==='Admin') {
$this->load->view('dashboard_view');
} else {
echo "Access Denied";
}
}
}
我已成功创建注册并登录。现在我想要 2 个角色,即管理员和用户。请帮助如何进行。
我的 SQL 表包含: 用户、电子邮件、密码、验证密钥、is_verified、角色(管理员和用户)。
【问题讨论】:
标签: php mysql sql codeigniter