How to create a password for a .htpasswd file using PHP

A user emailed me and asked how to create a password for a .htpasswd file using PHP. It is actually very simple.
Below is a PHP script that generates a password for .htpasswd from the clear text password stored in $clearTextPassword.

Please note: For Apache servers running on Windows you have to use the htpasswd program to generate passwords, or use the htpasswd generator.
<?php
// Password to be encrypted for a .htpasswd file
$clearTextPassword = 'some password';

// Encrypt password
$password = crypt($clearTextPassword, base64_encode($clearTextPassword));

// Print encrypted…

Leer artículo completo →