Jump to content

How To Have More Than 32 User Groups?


rain13

Recommended Posts

This function only works with groups which IDs are 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192, .... 2^32As you see this limits max groups to 32 or 64 depending on operating system. But is there way to have more than 32 or 64 usergroups?

function IsGroupMember($iUserGroup, $iGroup){    $iUserGroup = intval ($iUserGroup);$iGroup = intval ($iGroup);    if($iUserGroup & $iGroup) {return true;}    else {return false;}}

Because when you have groups IDs like 1,2,3,4,5,6,7,8 then that code would give false information. Wouldn't it?

Link to comment
Share on other sites

im not really sure what you're talking about. Could you at least provide the context of this function? I don't think anyone here is going to know what you're talking about, and the only thing native in your code post is intval, which to my knowledge, has no relation to the question you are asking about, so I don't think it's about that, right?

Link to comment
Share on other sites

i think he's referring to assigning 1 bit for each group, and AND'ing the user's group number with the actual group number, which will only work up to 32 or 64 bit numbers.i also considered this before, but ended up choosing a different method in the end. i think it's possible for it to work if you store the group numbers as strings in the db, then use php's bcxx functions or a loop to determine the bit position in the string or something.but i'd recommend just having a table called like user_groups with 2 fields, user_id, and group_id (both foreign keys), where for example, if a user is in 5 groups then there are 5 rows in that table for that user.

Link to comment
Share on other sites

It's not a great idea to use a bit mask if you have an arbitrary number of groups. Bit masks are necessarily limited to the precision of the CPU. You can have two arrays of "bits" that you loop through and use & on each pair. You can also store your values to check in an array and use in_array to test if a certain value is there.

Link to comment
Share on other sites

@thescientist thaf function checks if user is in given group so that every group has it's number and then user has sum of groups. so if we have: user = 1mod = 2admin = 4 then if I want to check if user is admin IsGroupMember(UserGroup,4) By that logic user with both: admin and mod rights would be 2 + 4 = 6 Just wondering ho other sites work. phpbb has only 8 bit int for group ID http://wiki.phpbb.co...le.phpbb_groups but still It can use more than 32 groups.

You can also store your values to check in an array and use in_array to test if a certain value is there.
Is there such variable type as array for SQL? Having N rows for every user would take space + cpu power. And probably bandwidth too. Edit: I figured that if ID of last group is 2^32 then this algorithm wouldn't allow me to have an other group for user because if 2^32 = admin and 2^31 = mod then admin + mod > 2^32 which means user could be admin or mod but cant be admin + mod. So the actual limit is 2^31 because with 31 groups user could be in all groups at once because the sum of 31 groups IDs = (2^32 -1) which fits in 32 bit int.
Link to comment
Share on other sites

Having N rows for every user would take space + cpu power. And probably bandwidth too.
I think databases are optimized for that sort of thing, especially with foreign keys.GROUP_CONCAT() can turn multiple mysql_fetch_row() calls into 1.I actually reckon it would use less cpu power, and far less space when the group ID gets high.
Link to comment
Share on other sites

How how to store users groups sum then? If user is in all 256 groups then sum of all group IDs > 256. I tried some googleing but didn't find any good example. Could anyone one explain how to work with multiple groups? if user has only 1 group then it's easy, but how to tell if user is member of multiple groups w/o creating n rows for user?

Link to comment
Share on other sites

It's a new table. Yes, it will have one record for each user-group association, but that doesn't matter. You can easily select all the users from a group, or all the groups that a single user belongs to by querying that table. You can also join the table with others in a query to get data about all the groups that a user belongs to.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...