What you should be looking at is bitmasks - much, much faster and shorter. Consider the binary representations of your levels:
Code:
1 = 0001
2 = 0010
4 = 0100
8 = 1000
Now say you wish to grand someone rank 1 and 4:
PHP Code:
$rank = 1 | 4; // Set all bits that are on in 1 or 4.
echo $rank; // Outputs 5, which is 0101 in binary
It really is that easy! Also check out the page on
bitwise operators in the manual.
The beauty is, you can also do this already in your SQL query:
Code:
SELECT `username`
FROM `accounts`
WHERE (`accessMask` & 4)
(This selects someone who will at least have access mask 4).
Little more complicated: someone who is both an access mask 4 and mask 2:
Code:
SELECT `username`
FROM `accounts`
WHERE (`accessMask` & (4 | 2))
Or the other way around, someone who does not have access right 8:
Code:
SELECT `username`
FROM `accounts`
WHERE ((`accessMask` ^ 8) & 8)
The adventage over what you are doing is that you can suffice with a single bitwise operation, which is much, much faster than several string comparisons. Additionally, you can simply enforce access rights directly from your database instead of having a PHP script parse them.
On a sidenote: for added readability you should define the various access masks as a class constant:
PHP Code:
class Login {
const ACCESS_READ = 1;
const ACCESS_NEWS = 2;
const ACCESS_ADMIN = 4;
public function __construct () {
$userMask = self::ACCESS_READ | self::ACCESS_NEWS;
}
}