用来解析.htpasswd文件的PHP类


Posted in PHP onSeptember 05, 2012

.htpasswd 文件示例:
user1:{SHA}kGPaD671VNU0OU5lqLiN/h6Q6ac=
user2:{SHA}npMqPEX3kPQTo+x/+ZckHDrIcQI=
user3:{SHA}q1Fh2LTUjjkncp11m0M9WUH5Zrw=

class Htpasswd { 
private $file = ''; 
private $salt = 'AynlJ2H.74VEfI^BZElc-Vb6G0ezE9a55-Wj'; 
private function write($pairs = array()) { 
$str = ''; 
foreach ($pairs as $username => $password) { 
$str .= "$username:{SHA}$password\n"; 
} 
file_put_contents($this -> file, $str); 
} 
private function read() { 
$pairs = array(); 
$fh = fopen($this -> file, 'r'); 
while (!feof($fh)) { 
$pair_str = str_replace("\n", '', fgets($fh)); 
$pair_array = explode(':{SHA}', $pair_str); 
if (count($pair_array) == 2) { 
$pairs[$pair_array[0]] = $pair_array[1]; 
} 
} 
return $pairs; 
} 
private function getHash($clear_password = '') { 
if (!empty($clear_password)) { 
return base64_encode(sha1($clear_password, true)); 
} else { 
return false; 
} 
} 
public function __construct($file) { 
if (file_exists($file)) { 
$this -> file = $file; 
} else { 
die($file." doesn't exist."); 
return false; 
} 
} 
public function addUser($username = '', $clear_password = '') { 
if (!empty($username) && !empty($clear_password)) { 
$all = $this -> read(); 
if (!array_key_exists($username, $all)) { 
$all[$username] = $this -> getHash($clear_password); 
$this -> write($all); 
} 
} else { 
return false; 
} 
} 
public function deleteUser($username = '') { 
$all = $this -> read(); 
if (array_key_exists($username, $all)) { 
unset($all[$username]); 
$this -> write($all); 
} else { 
return false; 
} 
} 
public function doesUserExist($username = '') { 
$all = $this -> read(); 
if (array_key_exists($username, $all)) { 
return true; 
} else { 
return false; 
} 
} 
public function getClearPassword($username) { 
return strtolower(substr(sha1($username.$this -> salt), 4, 12)); 
} 
}

使用方法
$passwdHandler = new Htpasswd('/home/myuser/.htpasswd'); 
// Add a user with name 'user1' and password 'I prefer to use passphrase rather than password.' if it doesn't exist in .htpasswd. 
$passwdHandler -> addUser('user1', 'I prefer to use passphrase rather than password.'); 
// Delete the user 'user1' if it exists in .htpasswd. 
$passwdHandler -> deleteUser('user1'); 
// Check if user 'user1' exists in .htpasswd. 
if ($passwdHandler -> doesUserExist('user1')) { 
// User 'user1' exists. 
}
PHP 相关文章推荐
PHP 的几个配置文件函数
Dec 21 PHP
PHP中文件缓存转内存缓存的方法
Dec 06 PHP
那些年一起学习的PHP(二)
Mar 21 PHP
PHP删除数组中特定元素的两种方法
Jul 02 PHP
PHP-Fcgi下PHP的执行时间设置方法
Aug 02 PHP
初识PHP
Sep 28 PHP
PHP访问Google Search API的方法
Mar 05 PHP
php操作redis缓存方法分享
Jun 03 PHP
PHP中使用curl伪造IP的简单方法
Aug 07 PHP
php有效防止同一用户多次登录
Nov 19 PHP
Laravel实现构造函数自动依赖注入的方法
Mar 16 PHP
使用ThinkPHP生成缩略图及显示
Apr 27 PHP
用来解析.htgroup文件的PHP类
Sep 05 #PHP
PHP curl 并发最佳实践代码分享
Sep 05 #PHP
PHP输出数组中重名的元素的几种处理方法
Sep 05 #PHP
PHP中使用crypt()实现用户身份验证的代码
Sep 05 #PHP
通过缓存数据库结果提高PHP性能的原理介绍
Sep 05 #PHP
PHP中使用foreach和引用导致程序BUG的问题介绍
Sep 05 #PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 #PHP
You might like
php 操作excel文件的方法小结
2009/12/31 PHP
利用Memcached在php下实现session机制 替换PHP的原生session支持
2010/08/21 PHP
php微信开发之带参数二维码的使用
2016/08/03 PHP
PHP + plupload.js实现多图上传并显示进度条加删除实例代码
2017/03/06 PHP
实例讲解PHP验证邮箱是否合格
2019/01/28 PHP
Yii2框架中一些折磨人的坑
2019/12/15 PHP
字符串的replace方法应用浅析
2011/12/06 Javascript
js图片自动切换效果处理代码
2013/05/07 Javascript
JavaScript代码生成PDF文件的方法
2016/02/26 Javascript
javascript实现秒表计时器的制作方法
2017/02/16 Javascript
JS查找数组中重复元素的方法详解
2017/06/14 Javascript
jQuery事件对象的属性和方法详解
2017/09/09 jQuery
简单了解Ajax表单序列化的实现方法
2019/06/14 Javascript
解决vue-photo-preview 异步图片放大失效的问题
2020/07/29 Javascript
[02:09]EHOME夺得首届辉夜杯冠军—现场颁奖仪式
2015/12/28 DOTA
[01:03:27]Optic vs VGJ.S 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python基础篇之初识Python必看攻略
2016/06/23 Python
深入理解NumPy简明教程---数组1
2016/12/17 Python
python DataFrame 修改列的顺序实例
2018/04/10 Python
Python 实现删除某路径下文件及文件夹的实例讲解
2018/04/24 Python
Python文本统计功能之西游记用字统计操作示例
2018/05/07 Python
对python中for、if、while的区别与比较方法
2018/06/25 Python
python对矩阵进行转置的2种处理方法
2019/07/17 Python
python实现上传文件到linux指定目录的方法
2020/01/03 Python
Pytorch模型迁移和迁移学习,导入部分模型参数的操作
2021/03/03 Python
英国儿童家具专卖店:GLTC
2016/09/24 全球购物
临床医学专业毕业生的自我评价
2013/10/17 职场文书
转党组织关系介绍信
2014/01/08 职场文书
父亲生日宴会答谢词
2014/01/10 职场文书
滞留工资返还协议书
2014/10/19 职场文书
小学语文继续教育研修日志
2015/11/13 职场文书
《春酒》教学反思
2016/02/22 职场文书
青年岗位能手事迹材料(2016推荐版)
2016/03/01 职场文书
导游词之安徽醉翁亭
2020/01/10 职场文书
关于maven依赖 ${xxx.version}报错问题
2022/01/18 Java/Android
Python中的 enumerate和zip详情
2022/05/30 Python