Jump to content

Getting/setting bits and flags


Brandon Jaeger

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/123351-gettingsetting-bits-and-flags/
Share on other sites

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...
}
?>

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.