obtaining the groups of a member through PHP

Hi,

I am using files htaccess, htpasswd and htgroups to control access to directories and files.

I want to use PHP to get the details of the user that is logged in (to display on page and use in an IF statement).

I am using $_SERVER['REMOTE_USER'] for the user, but I also want to know the groups that the user belongs to.
This way, I could say (for example):

if user_group = 'VIP' then print 'hello VIP'.

Does anyone know how to do this?

Regards,

TC
tchurchAsked:
Who is Participating?
 
ShelfieldCollegeCommented:
My first thought would be that if PHP doesn't have any inbuilt methods of retrieving the groups associated with a user, you could read the contents of the group file (typically htgroup etc) and collect them yourself.

<?

$grouplist = file('./path/to/htgroups');

$groups = array();

for ($i = 0; $i < count($grouplist); $i++) {

  $line = $grouplist[$i];
  $line = str_replace("\n", "", $line);
  $line = str_replace("\r", "", $line);

  if (substr($line, 0, 1) <> "#" ) {
    list($curgroup, $userlist) = split(':', $line);
    $users = split(',', $userlist);

    for ($j = 0; $j < count($users); $j++) {
      $groups[$curgroup][$users[$j]] = 1;
    }
  }
}

echo "<pre>"; print_r($groups); echo "</pre>";

?>

When I tested this it read the contents of an example htgroups file shown here...

# Example .htgroup file
group1:user1,user2,user3
group2:user2

and created a multidimensionall array of groups and the users in that groups in the format of:

$groups[groupname][username]  where groupname and username are the individual group names and user names respectively.

The example output from print_r($groups) is as follows:

Array
(
    [group1] => Array
        (
            [user1] => 1
            [user2] => 1
            [user3] => 1
        )

    [group2] => Array
        (
            [user2] => 1
        )

)

Then you can simple check if a user is in a group using isset($groups['groupname']['username']) e.g. isset($groups['matt']['vip'])

I hope this has been of some help, if you have any other questions please ask :)

Cheers

/Matt

P.S. Obviously this method is only usable if PHP doesn't have any internal functions that retrieve the groups a user is in which I'm unsure of.
0
 
ShelfieldCollegeCommented:
I forgot to mention that the   if (substr($line, 0, 1) <> "#" ) {  line is used to check whether the current line being processed is a comment or not, if you'r comments start with anything other than a # (not sure whether the htgroups file accepts other comment types but it could do) then simply expand this if condition to accomodate them e.g.

if ( (substr($line), 0, 1) <> "#") AND (substr($line, 0, 1) <> "/") ) {

The above would be used if your comments start with either a # or a /  however I recommend using # for comments in such files as this, that's if your file is commented at all.

Thanks
0
 
tchurchAuthor Commented:
Hi,

Would this also work if my groups file is set out differently?

For example:

web_admin:user1
Power_Users:user3
Power_Users:user1
Admin:user3
Admin:shane
Admin:user1
Members:user3
Members:user2
Members:user1
Members:user4

I cannot change the format of the groups file, because it is created by another program I have and I don't want to make that go wrong.

Let me know.

Thanks

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ShelfieldCollegeCommented:
Let me just test it to see, 2 secs.
0
 
ShelfieldCollegeCommented:
I just testing it using the below groups file:

# Example .htgroup file
group1:user1
group1:user2
group1:user3
group2:user1
group2:user2

and got the following result

Array
(
    [group1] => Array
        (
            [user1] => 1
            [user2] => 1
            [user3] => 1
        )

    [group2] => Array
        (
            [user1] => 1
            [user2] => 1
        )

)

So thankfully, yes it appears to work fine with the groups layout you use as well,

Cheers
0
 
tchurchAuthor Commented:
You are wonderful!!

It works like a charm.

Many Thanks.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.