Table of Contents
Based on native PHP cross-membership permission control
Home Backend Development PHP Tutorial Cross-membership permission control based on native PHP_PHP tutorial

Cross-membership permission control based on native PHP_PHP tutorial

Jul 13, 2016 am 10:21 AM
member Permissions

Based on native PHP cross-membership permission control

For a website's backend management system, a single super administrator authority often cannot meet our needs. Especially for large websites, this single authority will cause many problems.
For example: a website editor is usually only responsible for announcement updates of the company website, but if the website background does not have strict permission restrictions, he will be able to operate some of the customer's information. This is a big hidden danger.
If you have studied the ThinkPHP framework, you must know that there is something called RBAC. Today we will not talk about that, but let’s talk about how to implement cross permission control in the native PHP language.
Okay, not much to say, as usual, just talk about the principles and code.
There are many ways to implement cross-control of permissions. Here is just one idea: (I use the binary number method)
1. Here we first mention the operation methods of bitwise AND and bitwise OR:
1. Bitwise AND operator (&)
The two data participating in the operation are ANDed according to the binary bits. ("AND" operation => Whether there is a contained value such as: 7&8=0)
Operation rules: 0&0=0; 0&1=0; 1&0=0; 1&1=1;
That is: if both bits are "1" at the same time, the result is "1", otherwise it is 0
For example: 3&5 is 0000 0011 & 0000 0101 = 0000 0001 Therefore, 3&5 is worth 1.
In addition, negative numbers participate in bitwise AND operations in two's complement form.
2. Bitwise OR operator (|)
The two objects participating in the operation perform an "OR" operation based on binary bits. ("OR" operation => can include values ​​such as: 7=4|2|1, use "XOR" to remove included values ​​such as: 7^2)
Operation rules: 0|0=0; 0|1=1; 1|0=1; 1|1=1;
That is: as long as one of the two objects participating in the operation is 1, its value is 1.
For example: 3|5 that is 0000 0011 | 0000 0101 = 0000 0111 Therefore, 3|5 is worth 7.
In addition, negative numbers participate in bitwise OR operations in two's complement form.
After understanding the operations of bitwise AND and bitwise OR, let’s look at the following example:
Copy code
1
2 define('ADD',1);//Binary 1
3 define('DELETE',2);//Binary 10
4 define('UPDATE',4);//Binary 100
5 define('SELECT',8);//Binary 1000
6
7 //With permission it is 1, if there is no permission it is 0
8 $admin=ADD|DELETE|UPDATE|SELECT;//1111
9 $editor=ADD|UPDATE|SELECT;//1101
10 $user=SELECT;//1000
11 ?>
Copy code
I made four permissions for addition, deletion, modification and search respectively and set them as constants
The binary number of 1 is 1, the binary number of 2 is 10, the binary number of 4 is 100, and the binary number of 8 is 1000. This just becomes a rule
Some friends may ask where the 1111, 1101, and 1000 corresponding to the above permission variables admin, editor, and user come from?
There is a function in PHP to convert decimal numbers to binary numbers called decbin()
The following is the corresponding function explanation:
Copy code
decbin
(PHP 3, PHP 4, PHP 5)
decbin -- convert decimal to binary
Description
string decbin (int number)
Returns a string containing the binary representation of the given number parameter. The maximum value that can be converted is 4294967295 in decimal, which results in a string of 32 ones.
Example 1. decbin() example
echo decbin(12) . "n";
echo decbin(26);
?>
The above example will output:
1100
11010
See bindec(), decoct(), dechex() and base_convert().
Copy code
Let’s test the output and see:
Copy code
1
2
3
4 define('ADD',1);//Binary 1
5 define('DELETE',2);//Binary 10
6 define('UPDATE',4);//Binary 100
7 define('SELECT',8);//Binary 1000
8
9 //If there is permission, it is 1, if there is no permission, it is 0
10 $admin=ADD|DELETE|UPDATE|SELECT;//1111 15
11 $editor=ADD|UPDATE|SELECT;//1101 13
12 $user=SELECT;//1000 8
13
14 echo decbin($admin)."
";
15 echo decbin($editor)."
";
16 echo decbin($user)."
";
17
18
19 ?>
Copy code
Output result:
Then we can use this operation to determine the permissions. 1 means there is permission, 0 means no permission
For example:
The authority of admin (super administrator) is to add, delete, modify, and check, which is 1111——>0000 1111
The editor (website editor) has the permissions to add, modify, and check, which is 1101——>0000 1101
user (ordinary user) only has browsing and query permissions, which is 1000——>0000 1000
Then we only need to perform bitwise AND operations on them to determine whether we have permission
For example: (Looking from back to front) Convert decimal (database storage type value) to binary and perform "AND" operation
Website editing permissions 0000 1101 (the decimal value of the permission is 13) & 0000 0010 (the deletion permission is 2 in decimal and converted to 10 in binary). Result: 0000 0000, which means no permissions
Try again
Normal user permissions 0000 1000 & 0000 0001 (adding permissions in decimal is 1 and binary is 1) Result: 0000 0000 also does not have permissions
Super administrator permissions 0000 1111 & 0000 1101 (website editing permissions) Result: 0000 1101, which means you have website editing permissions
Okay, let’s look at specific examples
I built a database with 2 tables in it
One is the user table:
gid represents the group id of the permission table
One is the permission table:
flag represents the permission to add, delete, modify and check, which can be defined according to your own needs
Basic configuration page: config.php
Copy code
1
2
3 define('HOST','localhost');
4 define('DBNAME','member');
5 define('USER', 'root');
6 define('PASS', '');
7
8
9 $link=@mysql_connect(HOST,USER,PASS) or die('Database connection failed');
10
11 mysql_select_db(DBNAME,$link);
12
13 define('ADD',1);//binary 1
14 define('DELETE',2);//Binary 10
15 define('UPDATE',4);//Binary 100
16 define('SELECT',8);//Binary 1000
17
18 //If there is permission, it is 1, if there is no permission, it is 0
19 $admin=ADD|DELETE|UPDATE|SELECT;//1111
20 $editor=ADD|UPDATE|SELECT;//1101
21 $user=SELECT;//1000
22 ?>
Copy code
Log in homepage: index.html
Copy code
1
 2
 3
 4    
 5     Document
 6
 7
 8    
 9         账号:
10         密码:
11            
12        
13
14
复制代码
提交页面:action.php
 
复制代码
 1
 2     
 3     require_once('config.php');
 4     $username=$_POST['username'];
 5     $password=$_POST['password'];
 6 
 7 
 8     $sql="select * from user as a,role as b where a.gid=b.gid 
 9     and a.username='$username' and password='$password'";
10 
11     $result=mysql_query($sql);
12     if($data=mysql_fetch_array($result)){
13         //账号验证通过,判断对应权限
14         //此处判断的是 是否具备删除权限 如:user数据库存储的值为8转二进制为1000 删除权限的值为2转二进制为0010 与运算0000 无权限
15         if($data['flag']&DELETE){
16             echo "你有删除权限";
17         }else{
18             echo "你没有删除权限";
19         }
20 
21     }else{
22         echo "错误账号密码";
23     }
24     
25 
26 ?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/862109.htmlTechArticle基于原生PHP交叉会员权限控制 对于一个网站的后台管理系统,单一的超级管理员权限往往不能满足我们的需求,尤其是对于大型网站而言,...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to buy WeChat reading membership cheaply? Share the best way to buy membership on WeChat Reading! How to buy WeChat reading membership cheaply? Share the best way to buy membership on WeChat Reading! Mar 16, 2024 am 08:22 AM

1. How to buy WeChat reading membership cheaply? Share the best way to buy membership on WeChat Reading! 1. Open the WeChat Reading APP. There is a reading challenge in the reading welfare special session. Participate in the reading challenge. 2. Pay 1 yuan to participate, read for 7 days, duration >7 hours, and get a 4-day paid membership card with 4 book coins. You can participate for about 52 weeks in a year. If you participate every time, it will cost a total of 52 yuan, and you can get a total of 208 days. Paid membership card 208 book coins. 3. Pay 3 yuan to participate, read for 14 days, duration >14 hours, and get a 10-day paid membership card with 10 book coins. You can participate about 26 times a year. If you participate every time, it will cost a total of 78 yuan, and you can get a total of 260 days. Paid membership card costs 260 book coins. 4. Pay 4 yuan to participate and read for 21 days

Enable root permissions with one click (quickly obtain root permissions) Enable root permissions with one click (quickly obtain root permissions) Jun 02, 2024 pm 05:32 PM

It allows users to perform more in-depth operations and customization of the system. Root permission is an administrator permission in the Android system. Obtaining root privileges usually requires a series of tedious steps, which may not be very friendly to ordinary users, however. By enabling root permissions with one click, this article will introduce a simple and effective method to help users easily obtain system permissions. Understand the importance and risks of root permissions and have greater freedom. Root permissions allow users to fully control the mobile phone system. Strengthen security controls, customize themes, and users can delete pre-installed applications. For example, accidentally deleting system files causing system crashes, excessive use of root privileges, and inadvertent installation of malware are also risky, however. Before using root privileges

How to get qq music membership for free? Tutorial on getting QQ Music membership for free How to get qq music membership for free? Tutorial on getting QQ Music membership for free Mar 13, 2024 pm 08:37 PM

QQ Music is a music-listening software used by many users. Some songs here require users to have membership before they can download and play them. So how to get QQ Music membership for free? Let this site give users a detailed introduction to the tutorial on how to obtain QQ Music membership for free. Tutorial on getting QQ Music membership for free 1. First, we open QQ Music. 2. Go to my homepage and click on the three horizontal lines in the upper right corner. 3. Click to open the free music listening mode here. 4. A 15-second advertisement will appear here. We only need to wait for the advertisement to end to get a thirty-minute membership experience. Experience time can be superimposed. 5. Obtain a membership after reading it. Receive 1 day of QQ music

Instructions for automatic renewal and cancellation of Kugou Music APP membership Instructions for automatic renewal and cancellation of Kugou Music APP membership Mar 19, 2024 pm 07:28 PM

How to cancel automatic membership renewal on Kugou Music APP? There are many users who have applied for Kugou Music’s VIP auto-renewal service. Later, they want to cancel this service, but they don’t know where to cancel it. Below, I will bring you a tutorial on how to cancel the auto-renewal of Kugou Music. I hope it will be helpful to everyone. . It is very simple to cancel automatic renewal in Kugou Music APP: just enter the member center, find the music package/luxury VIP option, select automatic renewal enabled, and then click to close renewal. 2. WeChat: As shown in the picture below, go to the payment page, click the three dots in the upper right corner, select the deduction service, click Kugou Music to close the service; 3. Alipay: Go to the settings page, select payment settings, and select password-free payment/ Automatically deduct fees, and finally choose Kugou Music to terminate the contract.

How to cancel automatic renewal for Zhihu app members How to cancel automatic renewal for Zhihu app members How to cancel automatic renewal for Zhihu app members How to cancel automatic renewal for Zhihu app members Mar 13, 2024 am 11:04 AM

How to cancel the automatic renewal of Zhihu app membership? Zhihu app is a very practical mobile software. This software has many functions, and each function will bring a different feeling to the users. There are some contents on this software that require users to register as a member before they can read them. Membership on this software is not expensive, and continuous monthly membership will be cheaper. Some players want to know how to cancel automatic renewal. The editor below has compiled methods for canceling automatic renewal for your reference. How to cancel automatic renewal for Zhihu app members Zhihu members can choose four renewal methods, including Apple Pay, WeChat Pay, Alipay Pay and Baidu Pay. For users who choose Baidu Pay, renewal can be managed through WeChat or Alipay payment.

How to get Bilibili membership for free? Free for big members of B station How to get Bilibili membership for free? Free for big members of B station Mar 15, 2024 pm 05:00 PM

Bilibili is a video playback platform with rich resources, including a dance area, ghost animal area, food area, animal area, etc. But now many times you need to be a member to watch videos on site B. If you don’t want to spend money, can you get a member of site B? The editor here will bring you how to get the free membership of Bilibili. I hope it can help you. How to get free membership on Bilibili: Open Bilibili and click "My". Click the "Creation Home" icon in the "Creation Center" area. After entering the creation center, click "Task Center". After entering the task center, read the corresponding tasks and complete them to get points.

How to become a member of Xianyu_Introduction to how to activate VIP membership of Xianyu How to become a member of Xianyu_Introduction to how to activate VIP membership of Xianyu Mar 20, 2024 pm 05:50 PM

The "VIP membership" function in Xianyu APP is a value-added service provided to users. Users who become VIP members can enjoy a series of privileges and benefits, such as increased product exposure, more display opportunities, exclusive customer service, transaction guarantee upgrades, etc., which help improve users’ buying and selling experience and efficiency. How to become a member of Xianyu 1. First open the Xianyu software. After entering the homepage, you can switch to different pages. Here we click [My] in the lower right corner; 2. Then we can view many different pages in For information, we need to click [My Fish Value]; 3. After the final click, we can activate VIP membership on this page;

How to set permission access in QQ space How to set permission access in QQ space Feb 23, 2024 pm 02:22 PM

How to set permission access in QQ space? You can set permission access in QQ space, but most friends don’t know how to set permission access in QQ space. Next is the diagram of how to set permission access in QQ space brought by the editor for users. Text tutorial, interested users come and take a look! QQ usage tutorial QQ space how to set permission access 1. First open the QQ application, click [Avatar] in the upper left corner of the main page; 2. Then expand the personal information area on the left and click the [Settings] function in the lower left corner; 3. Enter the settings page Swipe to find the [Privacy] option; 4. Next in the privacy interface, select the [Permission Settings] service; 5. Then challenge to the latest page and select [Space Dynamics]; 6. Set up in QQ Space again

See all articles