PHP uses bit-and-operator & bitwise OR operator ' | ' to implement rights management

Source: Internet
Author: User

This is the permission value.
2^0=1, the corresponding 2 in the number of "0001″ (here ^ I expressed as a" sub-party ", that is: 2 of 0 parties, the same below)
2^1=2, the corresponding 2-in number is "0010″
2^2=4, the corresponding 2-in number is "0100″
2^3=8, the corresponding 2-in number is "1000″


To judge a number within certain numbers, you can use the & operator (the value is derived from the table above)
such as: 7=4|2|1 (You can also easily understand into 7=4+2+1)
Use & to operate, you can know 7&4, 7&2, 7&1 are true, and if 7&8 is False


Example:
<?php
Define Permissions
Define ("Mhight", 16);//higher permissions, 2 4-time
Define ("Mdelete", 8);//delete permission, 2 of 3 times
Define ("Mupload", 4);//upload permissions, 2 2-time
Define ("Mwrite", 2);//write permission, 2 1-time
Define ("Mread", 1);//Read-only, 2 0-time


Assign a value to a permission
Echo Mdelete|mupload|mwrite|mread;


if (& 16)
Echo ' has higher authority <br> ';

Judge permissions with & "bits and characters", assuming permission value $key
$key = 13;//13=8+4+1
if ($key & Mdelete)
Echo ' has delete permission <br> ';


if ($key & Mupload)
Echo ' has upload permission <br> ';


if ($key & Mwrite)
Echo ' has write permission <br> ';


if ($key & Mread)
Echo ' has read-only access <br> ';


//////////////////////////////////////////////////////////////
$key = 13;//13=8+4+1
if ($key & 8)
Echo ' has delete permission <br> ';


if ($key & 4)
Echo ' has upload permission <br> ';


if ($key & 2)
Echo ' has write permission <br> ';


if ($key & 1)
Echo ' has read-only access <br> ';

?>


One of the algorithms for the privilege score is this, which can be said to be simple and efficient. I do not know that we understand no, do not understand it is OK, remember the example on the line. The premise is to do a good job of distributing the permission values, that is, the 1, 2, 4, 8, 16 ... (there is also a sequential problem, the more advanced the permission is the higher the permission value, such as the above example demonstrates the deletion of the permissions). With the Permission distribution table can determine to a person what permissions, you simply understand to which permission to add the corresponding permission value it.
This method is very useful, the disadvantage is that if the permissions are distributed fine, then the authority value will be more and more large, you think about it, 2 of several parties, if all the permissions are all added. But that's enough for general permission.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.