This simple tutorial explains how to use a form to add users to a .htpasswd file.
You must have Perl installed on your hosting, and you must have permission to use the exec() php command!
First create a file called createuser.html - This page will have no php code,
just the form!
Create User
Before we start on the createuser.php file, that me just explain the format of the .htpasswd file:
. Each line containes on user.
. The username and password are seperated by a semicolon.
. The password is encrpyed.
A example is:
Jonathan:,9QjBEo0mBqAY
Lets get started on createuser.php:
// Get the values from the form
$username = $_POST['username'];
$password = $_POST['password'];
// This is the complicated bit. The result of a system command executed through the exec function is assigned to the variable
// $crpytpass. The system program that we are going to execute is Perl, and we want it to print the result of this:
// crypt("ThePassFromTheForm", ",9r-jd8,.") - The ",9r-jd8,." is just a way of encrypting that Apache understands.
// All encrypted passwords using this command will begin with ,9 - See the example above.
$cryptpass = exec("perl -e 'print crypt(\"$pass\",\",9r-jd8,.\")'");
// As mentioned above, the format of a htpasswd file is: Username:Encrpyed-pass, so we echo it out in that way.
echo $_POST['user'] . ":" . $cryptpass;
?> // End of script.