linux中解析.htpasswd文件的PHP类
发布:smiling 来源: PHP粉丝网 添加日期:2014-08-28 16:31:08 浏览: 评论:0
linux中解析.htpasswd文件的PHP类有需要的朋友可参考一下,介绍一个使用方法,实例代码如下:
- $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.
- }
htpasswd类,代码如下:
- 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}$passwordn";
- }
- 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 (!emptyempty($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 (!emptyempty($username) && !emptyempty($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; //开源代码phpfensi.com
- } else {
- return false;
- }
- }
- public function getClearPassword($username) {
- return strtolower(substr(sha1($username.$this -> salt), 4, 12));
- }
- }
Tags: linux解析 htpasswd文件
- 上一篇:php中异常处理方法总结
- 下一篇:PHP根据IP地址判断城市实现城市切换或跳转代码
相关文章
- ·PHP中通过Web 执行C/C++应用程序(2013-11-13)
- ·用PHP实现Ftp用户的在线管理(2013-11-13)
- ·用PHP自动把纯文本转换成Web页面(2013-11-13)
- ·用实例分析PHP5异常处理(2013-11-13)
- ·php5的simplexml解析错误(2013-11-13)
- ·PHP后门的隐藏技巧测试报告(2013-11-13)
- ·PHP缓存技术详谈(2013-11-27)
- ·构建可配置PHP应用程序的正确方式(2013-11-27)
- ·利用PHP自定义错误处理器处理出错信息(2013-11-27)
- ·PHP作wap开发时遇到的问题(2013-11-27)
- ·php编写大型网站问题集(2013-11-27)
- ·php测试性能代码(2013-11-28)
- ·vsftpd+mysql的虚拟用户配置记录(2013-11-28)
- ·FastCGI 不完全高级指南(PHP版,Windows平台)(2013-11-28)
- ·php 安全register globals设置为TRUE的危害(2013-11-28)
- ·PHP require/include顺序详解(2013-11-29)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)