With binary CONTROL permission code, int type of 32 bits, such as the total permission code to 1000, the fourth bit has permission, the other does not have permission, then how can I judge the permissions code, multiple permissions?

Source: Internet
Author: User
binary with the INT type 32 bits indicates that the bit has permission, the following 32-bit binary represents the permission code
All rights are: 11111111 11111111 11111111 11111111//(the first 00000 below can be omitted)
Only the lowest bit has permissions: 00000000 00000000 00000000 00000001
First place has the right: 00000000 00000000 00000000 00000010
Second place has permission: 00000000 00000000 00000000 00000100
Multiple permissions: 00000000 00000000 00000000 00000101//(this is the last and second person to have permission)
I want to ask is, if I 0th is to increase permissions, the first is to find permissions, the second is the update permission, the third is the delete permission,
But how can I tell when a single permission is granted, or given a permission code that is multiple permissions?
See who can write a method, put the obtained permission into the method can be judged?
I'm actually going to put it on the front page. Four check boxes for front-end display
Add Delete Modify Delete//If the backend is judged to have two permissions then the user enters the interface to see that two checkboxes are directly selected???? Do PHP to do the right card here to ask Daniel Guide??? 、?? /、????


Reply to discussion (solution)

? The practice of testing Linux limitations 777 755 .... The front, the root? It is possible to have a limited display.

segmentation, judging, should be these two steps up.

Most of the permissions I have seen are 1,1,1,1,1,1,1,1 so that there is a good distinction between the split symbols.

I do not know you that is the division, but if there is a concrete can run up the small example of the best, I can Bishi extension ...

Very simple, bit with

$ permissions = Array (  ' add ' = = 0b1, ' find ' = ' = ' 0b10 ',  ' update ' = = 0b100,  ' delete ' = = 0b1000, '); $ permission word = 0b101;foreach ($ Permissions as $k = = $v) {  printf ("%s
\ n ", $k, $ permission word & $v? ' Checked ': ', $k);}
Increase
Find
Update
Delete

$ permissions = Array (  ' add ' = = 0b1, ' find ' = ' = ' 0b10 ',  ' update ' = = 0b100,  ' delete ' = = 0b1000, '); $ permission word = 0b101;foreach ($ Permissions as $k = = $v) {  printf ("%s
\ n ", $v, $ permission word & $v? ' Checked ': ', $k);}
Increase
Find
Update
Delete

Taking into account multiple permissions, the second parameter of the POW can be increased upward. I don't know if you understand.

$add =pow (2,1);//2$del=pow (2,2);//4$update=pow (2,3);//8$query=pow (2,4)//16//Add & Remove $p= $add + $del; if (getstatus ($p, 1)) {//1 is query echo ' has increased permissions
';} Add & Delete & Modify $p= $add + $del + $update; if (GetStatus ($p, 3)) {//3 is modify echo ' have modified permission
';} if (!getstatus ($p, 4)) {//4 is query echo ' no query permission
';} function GetStatus ($status, $p) {$t = $status & Pow (2, $p)? 1:0;return $t;}

Five floor method I tried, my front-end code has an introduction to the format, his output put to the front end is always not put into the format, do not know what is going on, is not to be in printf (), before a bit of code or something, the foreground of my HTML code is as follows:




、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
Look at the source of the Web page they
When viewing the source page,Increase

Find

Update

Delete

Not in this block?? Solving

You should now be able to set 32 permissions to determine whether a single flag bit has this permission

GetAuth ($postStatus, $targetStatus) {    return $status & $targetStatus;} GetAuth (0x21f, 0x78);    Determine if there are 00000000 00000000 00000000 011111000 permissions

PHP in this way more harm than good, from a procedural point of view, seemingly did not play any optimization role, from the data structure, it is not easy to manage and find
Personal Thoughts

  • 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.