ozanhazer / php-htpasswd
A lightweight - single class - library to read and write htpasswd. You can add or delete users or you can update their passwords...
Requires
- php: ^7.2|^8.0
Requires (Dev)
- phpunit/phpunit: ^8|^9|^11
This package is auto-updated.
Last update: 2026-05-30 10:39:25 UTC
README
A lightweight PHP library for reading and writing Apache htpasswd files. You can add or delete users or update their passwords.
Requirements
- PHP 7.2 or higher (tested on 8.2–8.5)
Features
- 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.
Installation
composer require ozanhazer/php-htpasswd
Usage
The 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');
Encryption algorithms
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); // default
Different users in the same htpasswd file can use different algorithms.
See the Apache documentation for details on each format.
Tips
- 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+.