I saw in school a system that set permissions using binary string.
Let’s say 101001 = 41
So :
- 1 can be permission to page 1
- 2 can be permission to page 2
- 4 can be permission to page 3
- 8 can be permission to page 4
- 16 can be permission to page 5
- 32 can be permission to page 6
So let’s say I got the above binary string (101001). I have access to page 1, 4 and 6.
How can I do this in PHP ? Let’s say I got a field in MySQL named perms stored in dec so 101001 will be 41. How can I know that 41 is equal to 1, 8 and 32 in PHP ?
Thanks.
Sounds like you’re talking about bits and bit-wise operators. This easiest way to set this up is to define constants for every permission
Once you have these defined it’s easy to make comparisons using the bitwise operators:
This is how many of PHP’s own constants work. If you’ve ever set the error reporting using something like
E_ALL & E_DEPRECATEDyou’re actually working with binary numbers.