Home  >  Article  >  Backend Development  >  Steps to implement user authorization and role management using CodeIgniter framework

Steps to implement user authorization and role management using CodeIgniter framework

WBOY
WBOYOriginal
2023-07-28 22:42:531614browse

Steps to use the CodeIgniter framework to implement user authorization and role management

CodeIgniter is an open source PHP framework based on the MVC (Model-View-Controller) design pattern, suitable for quickly building web applications. User authorization and role management are very important parts when developing web applications. This article will introduce the steps to implement user authorization and role management using the CodeIgniter framework.

Step 1: Install the CodeIgniter framework
First, you need to download and install the CodeIgniter framework. You can download the latest version of the framework from the official website (https://codeigniter.com). After unzipping, place the framework files in the directory of your web server.

Step 2: Create database and data tables
Next, you need to create a database and create related data tables to store user and role information. The following is a simple example:

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `roles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `role_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `user_roles` (
  `user_id` int(11) NOT NULL,
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`user_id`, `role_id`),
  FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
  FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
);

Step 3: Configure database connection
In the CodeIgniter framework, you need to configure database connection information. In the application/config/database.php file, modify the following content:

$db['default'] = array(
    ...
    'hostname' => 'localhost',
    'username' => 'your_username',
    'password' => 'your_password',
    'database' => 'your_database',
    ...
);

Replace your_username, your_password, and your_databaseReplace with your database connection information.

Step 4: Create the model
In the CodeIgniter framework, the model is used to process data. In the application/models directory, create two files User_model.php and Role_model.php, and write the following content:

User_model.php

class User_model extends CI_Model {
    public function check_login($username, $password) {
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users');
        return $query->row();
    }

    public function get_user_roles($user_id) {
        $this->db->select('roles.role_name');
        $this->db->from('user_roles');
        $this->db->join('roles', 'user_roles.role_id = roles.id');
        $this->db->where('user_roles.user_id', $user_id);
        $query = $this->db->get();
        return $query->result();
    }
}

Role_model.php

class Role_model extends CI_Model {
    public function get_all_roles() {
        $query = $this->db->get('roles');
        return $query->result();
    }
}

Step 5: Create the controller
in application/controllersDirectory, create two files Auth.php and Admin.php, and write the following content:

Auth.php:

class Auth extends CI_Controller {
    public function login() {
        // 处理用户登录
    }

    public function logout() {
        // 处理用户注销
    }
}

Admin.php

class Admin extends CI_Controller {
    public function index() {
        $user_id = $this->session->userdata('user_id');
        $this->load->model('User_model');
        $data['user_roles'] = $this->User_model->get_user_roles($user_id);
        $this->load->view('admin/dashboard', $data);
    }
}

Step 6: Create a view
In the application/views directory, create login_form. php and dashboard.php two files, and write the following content:

login_form.php:

<form action="/auth/login" method="post">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <input type="submit" value="Login">
</form>

dashboard .php

<h1>Welcome to the Dashboard</h1>
<p>User Roles:</p>
<ul>
    <?php foreach($user_roles as $role): ?>
        <li><?= $role->role_name ?></li>
    <?php endforeach; ?>
</ul>

Step 7: Configure routing
In the application/config/routes.php file, configure the default route:

$route['default_controller'] = 'auth/login';

This Set the login method of the Auth controller as the default controller method.

Step 8: Test
Use a browser to access your web application and try to log in. If the login is successful, you will be redirected to the dashboard page with your user role information displayed.

Summary:
This article introduces the steps to use the CodeIgniter framework to implement user authorization and role management. By creating databases and data tables, configuring database connections, creating models, creating controllers and creating views, we can implement the functions of user login, logout and display of user roles. After learning the above steps, you can further expand and optimize user authorization and role management functions according to specific needs.

The above is the detailed content of Steps to implement user authorization and role management using CodeIgniter framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn