Table of Contents
Based on native PHP cross-member permission control, cross-member permission control
1. Bitwise AND operator (&)
2. Bitwise OR operator (|)
PHP中怎实现交叉会员?
php框架相对原生php影响性可以或效率
Home Backend Development PHP Tutorial Cross-membership permission control based on native PHP, cross-membership permissions control_PHP tutorial

Cross-membership permission control based on native PHP, cross-membership permissions control_PHP tutorial

Jul 13, 2016 am 10:21 AM
php member Native Backstage based on control Permissions manage net

Based on native PHP cross-member permission control, cross-member permission control

For a website’s backend management system, a single super administrator permission often cannot meet our needs, especially For large websites, this single permission can 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. Let’s talk about how to implement cross-authority 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.

Operation rules: 0&0=0; 0&1=0; 1&0=0; 1&1=1;

That is: if both two 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 complement form.

2. Bitwise OR operator (|)

The two objects participating in the operation perform an "OR" operation based on binary bits.

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 complement form.

After understanding the operations of bitwise AND and bitwise OR, let’s look at the following example:

<span> 1</span> <?<span>php
</span><span> 2</span>     <span>define</span>('ADD',1);<span>//</span><span>二进制1</span>
<span> 3</span>     <span>define</span>('DELETE',2);<span>//</span><span>二进制10</span>
<span> 4</span>     <span>define</span>('UPDATE',4);<span>//</span><span>二进制100</span>
<span> 5</span>     <span>define</span>('SELECT',8);<span>//</span><span>二进制1000
</span><span> 6</span> 
<span> 7</span> <span>    //有权限为1,没有权限为0</span>
<span> 8</span>     <span>$admin</span>=ADD|DELETE|UPDATE|SELECT;<span>//</span><span>1111</span>
<span> 9</span>     <span>$editer</span>=ADD|UPDATE|SELECT;<span>//</span><span>1101</span>
<span>10</span>     <span>$user</span>=SELECT;<span>//</span><span>1000</span>
<span>11</span> ?>
Copy after login

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 that converts decimal numbers to binary numbers called decbin()

The following is the corresponding function explanation:

<span>decbin</span><span>
(PHP </span>3, PHP 4, PHP 5<span>)

</span><span>decbin</span> --<span> 十进制转换为二进制
说明
</span><span>string</span> <span>decbin</span> ( int <span>number</span><span> )<br /><br />
返回一字符串,包含有给定 </span><span>number</span> 参数的二进制表示。所能转换的最大数值为十进制的 4294967295,其结果为 32 个 1<span> 的字符串。 

例子 </span>1. <span>decbin</span><span>() 范例
</span><?<span>php
</span><span>echo</span> <span>decbin</span>(12) . "\n"<span>;
</span><span>echo</span> <span>decbin</span>(26<span>);
</span>?><span>  

上例将输出:
</span>1100
11010<span>
参见 </span><span>bindec</span>(),<span>decoct</span>(),<span>dechex</span>() 和 <span>base_convert</span>()。 
Copy after login

Let’s test the output and see:

<span> 1</span> <?<span>php
</span><span> 2</span>     
<span> 3</span>     
<span> 4</span>     <span>define</span>('ADD',1);<span>//</span><span>二进制1</span>
<span> 5</span>     <span>define</span>('DELETE',2);<span>//</span><span>二进制10</span>
<span> 6</span>     <span>define</span>('UPDATE',4);<span>//</span><span>二进制100</span>
<span> 7</span>     <span>define</span>('SELECT',8);<span>//</span><span>二进制1000
</span><span> 8</span> 
<span> 9</span> <span>    //有权限为1,没有权限为0</span>
<span>10</span>     <span>$admin</span>=ADD|DELETE|UPDATE|SELECT;<span>//</span><span>1111</span>
<span>11</span>     <span>$editer</span>=ADD|UPDATE|SELECT;<span>//</span><span>1101</span>
<span>12</span>     <span>$user</span>=SELECT;<span>//</span><span>1000</span>
<span>13</span> 
<span>14</span>     <span>echo</span> <span>decbin</span>(<span>$admin</span>)."<br/>"<span>;
</span><span>15</span>     <span>echo</span> <span>decbin</span>(<span>$editer</span>)."<br/>"<span>;
</span><span>16</span>     <span>echo</span> <span>decbin</span>(<span>$user</span>)."<br/>"<span>;
</span><span>17</span> 
<span>18</span> 
<span>19</span> ?>
Copy after login

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 operation on them to determine whether we have permission

For example:

Website editing permissions 0000 1101 | 0000 0010 (Delete permissions are converted from 2 in decimal to 10 in binary) Result: 0000 0000 That is, no permissions are available

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 1111, 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

基本配置页面:config.php

<span> 1</span> <?<span>php
</span><span> 2</span>     
<span> 3</span>     <span>define</span>('HOST','localhost'<span>);
</span><span> 4</span>     <span>define</span>('DBNAME','member'<span>);
</span><span> 5</span>     <span>define</span>('USER', 'root'<span>);
</span><span> 6</span>     <span>define</span>('PASS', ''<span>);
</span><span> 7</span> 
<span> 8</span> 
<span> 9</span>     <span>$link</span>=@<span>mysql_connect</span>(HOST,USER,PASS) or <span>die</span>('数据库连接失败'<span>);
</span><span>10</span> 
<span>11</span>     <span>mysql_select_db</span>(DBNAME,<span>$link</span><span>);
</span><span>12</span> 
<span>13</span>     <span>define</span>('ADD',1);<span>//</span><span>二进制1</span>
<span>14</span>     <span>define</span>('DELETE',2);<span>//</span><span>二进制10</span>
<span>15</span>     <span>define</span>('UPDATE',4);<span>//</span><span>二进制100</span>
<span>16</span>     <span>define</span>('SELECT',8);<span>//</span><span>二进制1000
</span><span>17</span> 
<span>18</span> <span>    //有权限为1,没有权限为0</span>
<span>19</span>     <span>$admin</span>=ADD|DELETE|UPDATE|SELECT;<span>//</span><span>1111</span>
<span>20</span>     <span>$editer</span>=ADD|UPDATE|SELECT;<span>//</span><span>1101</span>
<span>21</span>     <span>$user</span>=SELECT;<span>//</span><span>1000</span>
<span>22</span> ?>
Copy after login

登陆首页:index.html

<span> 1</span> <span><!</span><span>DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span>></span>
<span> 2</span> <span><</span><span>html </span><span>xmlns</span><span>="http://www.w3.org/1999/xhtml"</span><span> xml:lang</span><span>="en"</span><span>></span>
<span> 3</span> <span><</span><span>head</span><span>></span>
<span> 4</span>     <span><</span><span>meta </span><span>http-equiv</span><span>="Content-Type"</span><span> content</span><span>="text/html;charset=UTF-8"</span><span>></span>
<span> 5</span>     <span><</span><span>title</span><span>></span>Document<span></</span><span>title</span><span>></span>
<span> 6</span> <span></</span><span>head</span><span>></span>
<span> 7</span> <span><</span><span>body</span><span>></span>
<span> 8</span>     <span><</span><span>form </span><span>action</span><span>="action.php"</span><span> method</span><span>="post"</span><span>></span>
<span> 9</span>         账号:<span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="username"</span> <span>/></span>
<span>10</span>         密码:<span><</span><span>input </span><span>type</span><span>="password"</span><span> name</span><span>="password"</span> <span>/></span>
<span>11</span>         <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="submit"</span><span> value</span><span>="登陆"</span><span>></span>    
<span>12</span>     <span></</span><span>form</span><span>></span>    
<span>13</span> <span></</span><span>body</span><span>></span>
<span>14</span> <span></</span><span>html</span><span>></span>
Copy after login

提交页面:action.php

<span> 1</span> <?<span>php
</span><span> 2</span>     
<span> 3</span>     <span>require_once</span>('config.php'<span>);
</span><span> 4</span>     <span>$username</span>=<span>$_POST</span>['username'<span>];
</span><span> 5</span>     <span>$password</span>=<span>$_POST</span>['password'<span>];
</span><span> 6</span> 
<span> 7</span> 
<span> 8</span>     <span>$sql</span>="<span>select * from user as a,role as b where a.gid=b.gid 
</span><span> 9</span>     and a.username='<span>$username</span>' and password='<span>$password</span>'"<span>;
</span><span>10</span> 
<span>11</span>     <span>$result</span>=<span>mysql_query</span>(<span>$sql</span><span>);
</span><span>12</span>     <span>if</span>(<span>$data</span>=<span>mysql_fetch_array</span>(<span>$result</span><span>)){
</span><span>13</span>         <span>//</span><span>账号验证通过,判断对应权限
</span><span>14</span> <span>        //此处判断的是 是否具备删除权限</span>
<span>15</span>         <span>if</span>(<span>$data</span>['flag']&<span>DELETE){
</span><span>16</span>             <span>echo</span> "你有删除权限"<span>;
</span><span>17</span>         }<span>else</span><span>{
</span><span>18</span>             <span>echo</span> "你没有删除权限"<span>;
</span><span>19</span> <span>        }
</span><span>20</span> 
<span>21</span>     }<span>else</span><span>{
</span><span>22</span>         <span>echo</span> "错误账号密码"<span>;
</span><span>23</span> <span>    }
</span><span>24</span>     
<span>25</span> 
<span>26</span> ?>
Copy after login

效果图如下:

轻松搞定~

这里只是个简单的小DEMO演示,希望能起到抛砖引玉的作用,至于具体项目还需具体分析,权限控制毕竟是个很复杂的功能。

 

PHP中怎实现交叉会员?

不明白.,,...
 

php框架相对原生php影响性可以或效率

肯定会慢一点,但是这是可以接受的。因为采用框架造成的性能损失比较恒定,例如对于所有功能,使用框架和直编可能总是框架慢0.002毫秒。但是这种损失一般不需要在意。因为相对于框架的巨大好处,这种损耗是值得的。

利用框架可以大幅度提升开发效率
大幅度节约维护成本
更容易的项目交接

因此,宁可损失效率也选择框架。关于效率可以考虑升级服务器等手段来改善。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/860055.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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

List of handling fees for okx trading platform List of handling fees for okx trading platform Feb 15, 2025 pm 03:09 PM

The OKX trading platform offers a variety of rates, including transaction fees, withdrawal fees and financing fees. For spot transactions, transaction fees vary according to transaction volume and VIP level, and adopt the "market maker model", that is, the market charges a lower handling fee for each transaction. In addition, OKX also offers a variety of futures contracts, including currency standard contracts, USDT contracts and delivery contracts, and the fee structure of each contract is also different.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

See all articles