?
Solved

obtaining the groups of a member through PHP

Posted on 2005-04-13
6
Medium Priority
?
202 Views
Last Modified: 2006-11-18
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
0
Comment
Question by:tchurch
  • 4
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
ShelfieldCollege earned 800 total points
ID: 13780180
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
 
LVL 4

Expert Comment

by:ShelfieldCollege
ID: 13780198
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
 

Author Comment

by:tchurch
ID: 13825049
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:ShelfieldCollege
ID: 13825293
Let me just test it to see, 2 secs.
0
 
LVL 4

Expert Comment

by:ShelfieldCollege
ID: 13825329
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
 

Author Comment

by:tchurch
ID: 13825861
You are wonderful!!

It works like a charm.

Many Thanks.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question