4

I know that it's possible to add new HTTP auth credentials through this shell script:

htpasswd -c .htpasswd testing

Is it possible to achieve the same with a PHP script? I know I could use a regular PHP auth system, but that's not what I'm looking for.

DriesOeyen
  • 483
  • 6
  • 13
  • See http://stackoverflow.com/questions/2469197/how-to-generate-a-hash-like-apaches-htpasswd-using-java – Gerben Nov 28 '11 at 16:12

1 Answers1

3

You can execute this command with system or exec to add your users.

I've created a code snippet that might work for you:

<?php
define('HTPASSFILEPATH', '.htpasswd');

function addUser($pUser, $pPass)
{
    exec("htpasswd -cb " . HTPASSFILEPATH . " ${pUser} ${pPass}");
}
?>

And remember, the htpasswd tool should be in your PATH

0xd
  • 1,891
  • 12
  • 18
  • Thank you for your kind help. I tried to run your script with the system function instead of exec and used the return_var parameter to see what went wrong. It returns code 127. EDIT: Sorry, I think I may have set up the wrong path to the htpasswd tool. Let me try again. – DriesOeyen Nov 25 '11 at 23:46
  • WagnerVaz, I think I'm going to use a different auth system altogether. I'll always need the exact path to the htpasswd tool for this to work while what I'm looking for is something portable. Thank you for your time and your quick reply anyway, I appreciate it! – DriesOeyen Nov 25 '11 at 23:53
  • @DriesOeyen finding a path of an executable on Unix / Linux is pretty simple, you can use the tool `which`. So modifing the line of `exec` above adding `which` you can do without knowing the path of `htpasswd`. `exec("\`which htpasswd\` -cb " . HTPASSFILEPATH . " ${pUser} ${pPass}");` – 0xd Nov 26 '11 at 01:53