CoderZone.org

Category: >> PHP Code >> Write or update an htpasswd file in Apache Bookmark and Share

<< lastnext >>

Snippet Name: Write or update an htpasswd file in Apache

Description: Very handy- the "set_htpasswd" function can be used to write a new username and password for HTTP digest authentication with Apache.

Also see:
» Easy Password Encryption and Decry...
» USER password expiration
» USER password
» Verify Password
» Password setup
» Password Strength Tester
» Create Random Password
» Random Pronounceable Passwords
» Pronouncable password generator

Comment: (none)

Author:
Language: PHP
Highlight Mode: PHP
Last Modified: March 30th, 2011

// update an htpasswd file with username(s) and password(s)
 
function set_htpasswd($filename,$user,$pass){
 
        $password=array();
 
        if (file_exists($filename)){
                foreach(file($filename) as $line){
                        $pair = explode(':', $line);
                        $password[$pair[0]] = chop($pair[1]);
                }
        }
 
        $password[$user] = crypt($pass,CRYPT_STD_DES);
 
        $fp=fopen($filename,"w+");
        while(list($u,$p) = each($password))
                fputs($fp,"$u:$p\n");
        fclose($fp);
}
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 5