View Single Post
Old 08-13-2004, 06:00 PM   #3 (permalink)
bubbagumpshrimp
Crazy
 
Could you not just have a field in the Users table? Call it 'permissions' or something obvious like that. Rather than having a seperate field in the db for each section, you have a single int field, and using different bitmasks, your php app can work out who has permission to do what, without eating up tons of space in your db.

Example:

<Table: Users>
uId int auto_increment primary key
uName varchar not_null
uPass varchar not_null
... (etc) ...
uPermissions int not_null

Say you have 8 sections, and a user name 'bubba', who has a uId of 6. Bubba's uPermissions field holds the number 7. That's just great you say, but what does it mean?

Lets look at the binary; 7 turns into 0000 0111. Now we can see that bubba can access sections 1, 2, and 3. (we start from the right).

To figure this out using php, we can do something like this:
Code:
// user is trying to access section 2.  lets see if he's allowed.
$q = "select uPermissions from users where uId=6;" // recall that bubba's uid is 6
$rs = mysql_query($q,$db); // where db is an existing database connection
$row = mysql_fetch_array($rs);
$userPerm = $row['uPermissions'];

if(($userPerm & 2) == 2)
  echo "welcome to the section!"
else
  echo  "go away";

// for section 3, we'd & with 4.  for section 4, with 8, and so on, using each bit to show permission to a given section.
I hope that made some kind of sense.
there are a couple example of stuff at http://ca2.php.net/manual/en/languag...rs.bitwise.php

good luck
bubbagumpshrimp is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76