background image

$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