Brandon Jaeger Posted September 9, 2008 Share Posted September 9, 2008 Hi again, I'm trying to get flag information from a row in the table, read it, then assign bits to it. DEFINE ('ADMIN_NEWS' , 1); DEFINE ('ADMIN_SERVERS' , 2); DEFINE ('ADMIN_ALLIES' , 4); DEFINE ('ADMIN_CONTENT' , ; DEFINE ('ADMIN_COLUMNS' , 16); DEFINE ('ADMIN_USERS' , 32); function GetUserFlags($id) { $query = "SELECT * FROM users WHERE id = " . $id; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); $flags = $row["flags"]; $num = 0; if(strstr($flags, "a")) $flags = ($flags | ADMIN_NEWS); if(strstr($flags, "b")) $flags = ($flags | ADMIN_SERVERS); if(strstr($flags, "c")) $flags = ($flags | ADMIN_ALLIES); if(strstr($flags, "d")) $flags = ($flags | ADMIN_CONTENT); if(strstr($flags, "e")) $flags = ($flags | ADMIN_COLUMNS); if(strstr($flags, "f")) $flags = ($flags | ADMIN_USERS); return $flags; } I'm trying to use it like this: if(GetUserFlags($_SESSION["userid"]) & ADMIN_NEWS) echo "Success!"; What am I doing wrong? Thanks. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 Why don't you actually store the flags that they're allowed as the number in the database...? Sort of like chmod does for file permissions. EDIT: Example (using your established constants): Give user ALLIES, CONTENT, and USERS privileges: Flag: ALLIES + CONTENT + USERS = 4 + 8 + 32 = 44 Check if this person can edit users and allies (maybe you need them both on the same page for some reason, so that's why I'm giving this example): <?php $flag = 44; if ($flag & (ADMIN_ALLIES | ADMIN_USERS)) { //good! } else { //nope... } ?> Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 The reason I'm not storing the numbers in the database as you said is because I want them to be easily editable for any admin via text input in a form. For example, the admin puts "abcdef" in the form and it inputs it in the database. Then it sums up the bits according to the flags in the user's row and reads them and decides if the user has access or not. How would I do that? <3 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 How about you just make checkboxes and translate that into numbers and then just do simple addition? Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 Hmm yeah I actually thought about doing that. I'll put that into motion and see how it goes. I shall post my results back here Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 Okay, so I just did a little tinkering with the flags and I can't get them to work properly. function GetUserFlags($id) { $query = "SELECT * FROM users WHERE id = " . $id; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); $flags = $row["flags"]; return (int)$flags; } if(GetUserFlags($_SESSION["userid"]) & ADMIN_NEWS) echo "News flag detected"; What I did was added up the sum of all of the flags (63) and inserted that into the DB manually. It doesn't work, though... The session part is correct. I printed them out and it says 1 which is the ID I want. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 echo $flags and make sure it's what you expect. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 Yup. It says 63. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 I don't mean to be rude by this, but are you sure you included the constants on top? Because this: <?php if (63 & 1) { echo "good!\n"; } ?> Echo's "good!". Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 There's my problem! The constants are in config.php, which is included in header.php, which is included at the very top of this script. How do you make a global constant? ;x Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 No such thing, they should already be constant. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 Shouldn't it already be able to read it from a different file when it's included into the page? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 9, 2008 Share Posted September 9, 2008 Yes. Quote Link to comment Share on other sites More sharing options...
Brandon Jaeger Posted September 9, 2008 Author Share Posted September 9, 2008 Hmm well I switched some stuff around and for whatever reason, it works now. Thanks, dude Now to do the checkboxes. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.