I am a beginner with the htaccess use, i searched an answer on google but didn't find something good to me.
I would like to do something easy to understand (so I'm sure there is a proper way to do it) but don't know how to do.
I have those 3 files:
.htaccess
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /path/.htpasswd
AuthGroupFile /path/.htgroups
require group intern
require group extern
require group test
.htpasswd
user1:xxx
user2:yyy
user3:zzz
.htgroups
group1:user1 user3
group2:user2
And in php i would like to make a test on the group. I will have a lot of user in some groups and i will add and remove some so i don't want to do :
if($_SERVER['PHP_AUTH_USER'] == 'user1' || $_SERVER['PHP_AUTH_USER'] == 'user2')
but
if($group == 'group1')
Could you help finding an easy way to get this variable $group ?
Thanks a lot,
Bastien
After some research on php.net, i found a way by matching a pattern in a file (here in .htgoups), so i did this function to get a group of a logged user.
function getHtGroup(){
$AuthGroupFile = file("/path/.htgroups");
$user = $_SERVER['PHP_AUTH_USER'];
foreach ($AuthGroupFile as $line_num => $line) {
if(preg_match("/(.*):(.*)$user/", $line, $matches)){
$group=$matches[1];
break;
}
}
return $group;
}
Building on BastienSander's answer. These two functions will get the htgroup file path from the .htaccess file and get a group of the user.
function gethtgroupfilepath() {
preg_match('/AuthGroupFile .*/i', file_get_contents('.htaccess'), $match);
$htgroupfilepath = array_pop(preg_split('/AuthGroupFile /i', $match[0]));
return($htgroupfilepath);
}
function gethtgroup(){
$AuthGroupFile = file(gethtgroupfilepath());
$user = $_SERVER['PHP_AUTH_USER'];
foreach ($AuthGroupFile as $line_num => $line) {
if(preg_match("/(.*):(.*)$user/", $line, $matches)){
$group=$matches[1];
break;
}
}
return $group;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.