用来解析.htgroup文件的PHP类

所属分类: 网络编程 / PHP编程 阅读数: 1223
收藏 0 赞 0 分享
.htgroup 文件示例:
admin: user2
editor: user1 user3
writer: user3
复制代码 代码如下:

class Htgroup {
private $file = '';
private function write($groups = array()) {
$str = '';
foreach ($groups as $group => $users) {
$users_str = '';
foreach ($users as $user) {
if (!empty($users_str)) {
$users_str .= ' ';
}
$users_str .= $user;
}
$str .= "$group: $users_str\n";
}
file_put_contents($this -> file, $str);
}
private function read() {
$groups = array();
$groups_str = file($this -> file, FILE_IGNORE_NEW_LINES);
foreach ($groups_str as $group_str) {
if (!empty($group_str)) {
$group_str_array = explode(': ', $group_str);
if (count($group_str_array) == 2) {
$users_array = explode(' ', $group_str_array[1]);
$groups[$group_str_array[0]] = $users_array;
}
}
}
return $groups;
}
public function __construct($file) {
if (file_exists($file)) {
$this -> file = $file;
} else {
die($file." doesn't exist.");
return false;
}
}
public function addUserToGroup($username = '', $group = '') {
if (!empty($username) && !empty($group)) {
$all = $this -> read();
if (isset($all[$group])) {
if (!in_array($username, $all[$group])) {
$all[$group][] = $username;
}
} else {
$all[$group][] = $username;
}
$this -> write($all);
} else {
return false;
}
}
public function deleteUserFromGroup($username = '', $group = '') {
$all = $this -> read();
if (array_key_exists($group, $all)) {
$user_index = array_search($username, $all[$group]);
if ($user_index !== false) {
unset($all[$group][$user_index]);
if (count($all[$group]) == 0) {
unset($all[$group]);
}
$this -> write($all);
}
} else {
return false;
}
}
}

复制代码 代码如下:

$groupHandler = new Htgroup('/home/myuser/.htgroup');
// Add user 'user1' to group 'admin' in .htgroup. Group will be automatically created if it doesn't exist.
$groupHandler -> addUserToGroup('user1', 'admin');
// Delete user 'user1' from group 'admin' in .htgroup. Group will be automatically removed if it doesn't contain any users.
$groupHandler -> deleteUserFromGroup('user1', 'admin');
更多精彩内容其他人还在看

PHP中使用crypt()实现用户身份验证的代码

在开发PHP应用中如果不想自己开发新的加密算法,还可以利用PHP提供的crypt()函数来完成单向加密功能
收藏 0 赞 0 分享

PHP输出数组中重名的元素的几种处理方法

PHP输出数组中重名的元素的几种处理方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP curl 并发最佳实践代码分享

在实际项目或者自己编写小工具(比如新闻聚合,商品价格监控,比价)的过程中, 通常需要从第3方网站或者API接口获取数据, 在需要处理1个URL队列时, 为了提高性能, 可以采用cURL提供的curl_multi_*族函数实现简单的并发
收藏 0 赞 0 分享

用来解析.htgroup文件的PHP类

用来解析.htgroup文件的PHP类代码,需要的朋友可以参考下
收藏 0 赞 0 分享

用来解析.htpasswd文件的PHP类

有时候需要获取.htpasswd文件的内容,那么就可以用下面的代码类了,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP imagecreatefrombmp 从BMP文件或URL新建一图像

大家都知道php GD库可方便的从URL新建一图像, GD中有imagecreatefromjpeg(),imagecreatefromPNG()....等
收藏 0 赞 0 分享

php生成静态文件的多种方法分享

最近需要将自己写的php文件生成静态,经过查找,发现有多种方法,不同需求可以选择不同的方法
收藏 0 赞 0 分享

采集邮箱的php代码(抓取网页中的邮箱地址)

由于搞了个群发邮件的程序,当然没邮箱不行,所以写了个采集邮箱程序
收藏 0 赞 0 分享

一个简单的网页密码登陆php代码

密码对,就可以看到指定内容, 密码不对就进不去
收藏 0 赞 0 分享

UCenter 批量添加用户的php代码

有时候我们需要批量添加用户,只要把该php放进UCenter目录下,执行就可以了。
收藏 0 赞 0 分享
查看更多