Getting incorrect match on php bitwise operators

I am getting an incorrect match using bitwise operators in PHP and most likely a stupid mistake but cant see the forest for the trees type scenario atm.

I have 6 permissions (1,2,4,8,16,32) and a permissions field in user login (currently set to 6)

I have simple function to return that from DB… getUserdets($LIU[‘uid’],‘Permissions’) which if I echo returns correctly ‘6’

I then have a a list of all available permissions from DB and then use this to tick the ones user has permissions to…

if (getUserdets($LIU[‘uid’],‘Permissions’) & $row[1]) { echo " checked"; }

Which correctly shows 2 & 4 ticked but also 32, if I change it to…

if (6 & $row[1]) { echo " checked"; }

Then this shows correctly with just 2 & 4 ticked.

If I had any hair left I would be pulling it out at this point, can anyone help?

If you have 6 permissions, [1 ,2, 4,8, 16, 32], 6 should not be recognized at all.

That table should show userID 5’s permissions as

SELECT usrID, permission FROM user_permissions WHERE usrID = 5;

usrID | permission
5 | 2
5 | 4

In bitwise 6 = 4+2

Using MySQL to actually do the query works, the following functions should technically be the same…

[php]function mysql_perm($bit) {
global $LIU;
global $sqlcon;
$sql = “SELECT id FROM users WHERE id=’”.$LIU[‘uid’]."’ AND permissions & $bit";
$res = mysqli_query($sqlcon,$sql);
if (mysqli_num_rows($res) > 0) { return 1; }
}
function php_perm($bit) {
global $LIU;
if (getUserdets($LIU[‘uid’],‘Permissions’) & $bit) { return 1; }
}
[/php]

(have changed user permissions to 7 in this example)

So I now have two identical fetch row for the permissions table one using MYSQL and the other using PHP to check. (the user permissions numer [7] echoed is using getUserdets($LIU[‘uid’],‘Permissions’) to ensure this is definitely set correctly).

I get two totally different results…

--- MYSQL --- User permission [7] does have [1] access User permission [7] does have [2] access User permission [7] does have [4] access User permission [7] does not have [8] access User permission [7] does not have [16] access User permission [7] does not have [32] access

— PHP —
User permission [7] does have [1] access
User permission [7] does have [2] access
User permission [7] does have [4] access
User permission [7] does not have [8] access
User permission [7] does have [16] access
User permission [7] does have [32] access

Rather cut out an uncessary MYSQL query

Sponsor our Newsletter | Privacy Policy | Terms of Service