Detailed introduction to PHP's clever use of bit operations to achieve website permission management

黄舟
Release: 2023-03-06 15:14:02
Original
1424 people have browsed it

The following editor will bring you an article on how PHP cleverly uses bit operations to achieve website authority management. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let's follow the editor and take a look.

First of all, let's define 4 constants to set four permissions:

========================================

define(ADD,1);//Increase the permissions of database records
define(UPD,2);//Modify the permissions of database records
define(SEL ,4);//Permission to search database records
define(DEL,8);//Permission to delete database records

======= ==============================

Next Assume there are 3 users:

User A has four permissions ADD-UPD-SEL-DEL. Use bitwise OR operation to calculate the total value of A's permissions
$a_ all=ADD|UPD|SEL|DEL;//$all=15 You can notice that this value is the same as the result of addition
B user has the three permissions of ADD-UPD-SEL, use Bitwise OR operation calculates the total value of B's ​​permissions
$b_all=ADD|UPD|SEL;//$all=7 This value is the same as the result of addition
User C has two permissions, ADD-UPD. Use bitwise OR operation to calculate the total permission value of C
$c_all=ADD|UPD;//$all=3 This value is still the same as the result of addition

====== ===============================

Next we use Bitwise AND operation
$a_all&ADD The result is true
$a_all&UPD The result is true
$a_all&SEL The result is true
$a_all&DEL The result is true

======================================

$b_all&ADD The result is true
$b_all&UPD The result is true
$b_all&SEL The result is true
$b_all&DEL The result is false

##=== ==================================

$ c_all&ADD returns true$c_all&UPD returns true
$c_all&SEL returns false
$c_all&DEL returns false

##======= ==============================


Have you discovered the secret?

1. When the total value of permissions is ANDed with the permission that does not exist, the result is false


2. The values ​​of permissions are all Power of 2


After knowing these two points of permission processing, it becomes simple. Just add the total value of the user's current permissions and the current value every time an operation involving permissions is performed. The permissions required for the operation are bitwise ANDed. If the result is true, execute it, and if it is false, just report an error (of course, it does not necessarily report an error, you can design your own program when you don't have permission).

The above is the detailed content of Detailed introduction to PHP's clever use of bit operations to achieve website permission management. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template