Search code examples
php.htaccess.htpasswd

How can I use md5 passwords in .htpasswd file?


Hi I am trying to set up a .htaccess and a .htpasswd file to see if a user can have access to a particular directory. When a new user registers, their passwords are encrypted with the PHP md5() function and then stored in the database with their other login info. I would like to add a few of their encrypted passwords into my .htpasswd file dynamically so that they can enter into a sub member section. I am having trouble because the .htpasswd file would work with passwords encrypted with the crypt() function but i am not sure how to get it to work with their md5 encrypted passwords.


Solution

  • Documentation says:

    htpasswd encrypts passwords using either a version of MD5 modified for Apache, or the system's crypt() routine. Files managed by htpasswd may contain both types of passwords; some user records may have MD5-encrypted passwords while others in the same file may have passwords encrypted with crypt().

    I think you can explicitly specify apache to use md5s for password in htpasswd file using -m argument to htpasswd

    If you don't want or can't use the htpasswd tool, you can create crypt() based passwords from PHP:

    $clearTextPassword = 'some password';
    $password = crypt($clearTextPassword, base64_encode($clearTextPassword));