I need a bitwise permissions system (I think)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Everah wrote:What happens when you want to add a permission in between 4 and 8?
the permissions themselves wouldn't have a value between 4 and 8. A user level might though. Let's assume a user has a level of 5.

in binary that's:

Code: Select all

0101
that user would match a permission privilege of 4 and 1, but not 2 and 8.

the cool thing is you can save a user's permission level as an integer on the db (or in a session var) and compare it against the binary permissions using the & operator as Begby outlined above.

so for permission privilege '4':

a user with permission level 5 would match
a user with permission level 6 would match
a user with permission level 7 would match
a user with permission level 8 would not match
etc...
Post Reply