用来解析
.htpasswd 文件的 PHP 类
有时候需要获取
.htpasswd 文件的内容,那么就可以用下面的代码类了,需要的朋友可以参
考下
.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
)) {