A lightweight PHP library for reading and writing Apache htpasswd files. You can add or delete users or update their passwords.
- PHP 7.2 or higher (tested on 8.2–8.5)
- Supports
crypt, APR-MD5, SHA-1 and bcrypt algorithms - Locks the htpasswd file to prevent conflicts while writing
- Throws exceptions on invalid usernames
- Unit tested
Note: The entire htpasswd file is loaded into memory. If you have a very large number of users, consider a different authentication mechanism.
composer require ozanhazer/php-htpasswdThe Htpasswd class has no namespace, so it works in both non-namespaced and namespaced projects:
use Htpasswd;
$htpasswd = new Htpasswd('/path/to/.htpasswd');
$htpasswd->addUser('ozan', '123456');
$htpasswd->updateUser('ozan', '654321');
$htpasswd->deleteUser('ozan');Apache htpasswd supports three password formats. You can specify the algorithm when adding or updating a user:
$htpasswd->addUser('ozan', '123456', Htpasswd::ENCTYPE_APR_MD5);
$htpasswd->addUser('ozan', '123456', Htpasswd::ENCTYPE_SHA1);
$htpasswd->addUser('ozan', '123456', Htpasswd::ENCTYPE_BCRYPT);
$htpasswd->addUser('ozan', '123456', Htpasswd::ENCTYPE_CRYPT); // defaultDifferent users in the same htpasswd file can use different algorithms.
See the Apache documentation for details on each format.
- Avoid
ENCTYPE_CRYPTon Windows — it is not available by default. ENCTYPE_CRYPTpasswords are limited to 8 characters; extra characters are silently ignored. The library will trigger a notice if a longer password is provided.- SHA-1 (
ENCTYPE_SHA1) is considered weak. APR-MD5 is the most broadly compatible option; bcrypt (ENCTYPE_BCRYPT) is the strongest and is recommended for Apache 2.4+.