tags:

views:

14

answers:

1

How do I register a new username/password on apache (.htaccess and .htpasswds) using php?

A: 

If the user running the php script has permissions to write in your .htpasswd file (which, btw, can be a big security risk), you can write into it like you would with any regular file:

$filename = "path-to-htpasswd-file";
$content = $user.":".crypt($pass)."\r\n";

if (is_writable($filename)) {
    $handle = fopen($filename, 'a');
    if ($handle) {
        fwrite($handle, $somecontent);
        fclose($handle);
    }
}
A. M.