Php permission management

Source: Internet
Author: User
Assume that a decimal number 10 is used to obtain the & 20540; (10> 0) & amp; 0x01 percentile & 20540; (10> 1) & amp; 0x01 penultimate & 20540; assume that there are binary numbers 101 to 10 hexadecimal $ res = 0; $ res | = (1 "/> <scripttype =" t

Assume there is a decimal number 10.

Obtain the value of each binary value of 10:

(10> 0) & 0x01 // delimiter value

(10> 1) & 0x01 // The second-to-last value

Assume there are 101 binary numbers

Convert to 10 hexadecimal

$ Res = 0;

$ Res | = (1 <0); // the low position is 1.

$ Res | = (1 <1); // The value of high is 1.

// The intermediate bit is not 1

 Power as $ k =>v v) {if (in_array ($ v, $ user) {$ res |=( 1 <$ k );}} return $ res;} // obtain the permission list based on the int value function getPowerByInt ($ num) {$ res = array (); foreach ($ this-> power as $ k => $ v) {$ res [$ v] = ($ num >>$ k) & 0x01 ;} return $ res ;}}// assume that a user has the permission of P1 and P4 $ user = array ('p1', 'p4 '); $ test = new Test (); // view the int value of the user permission var_dump ($ test-> getPowerInt ($ user )); // output 9 // note that the order is in descending order. if a user has 0101 permissions, that is, only P1 and P3 are allowed, that is, decimal 5. // output the permission list var_dump ($ test-> getPower ByInt (5); // output array (4) {// ["P1"] => // int (1) // ["P2"] => // int (0) // ["P3"] => // int (1) // ["P4"] => // int (0) //} // ------------------ if the permission is insufficient, add the permission -------- // in reverse order. Add $ power/$ power = array (// "P1", // "P2", // "P3", // "P4 ", // "P5", //); // New test result: Old permissions are not affected. Old users do not have new permissions by default. // therefore, the role bit can be considered as the highest permission at the beginning of the design, all. // Var_dump ($ test-> getPowerInt ($ user); // output 9 // var_dump ($ test-> getPowerByInt (5); // output array (4) {// ["P1"] => // int (1) // ["P2"] => // int (0) // ["P3"] => // int (1) // ["P4"] => // int (0) // ["P5"] => // int (0 )//}



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.