<?php

$file = '.htpasswds';
$login = $_SERVER['REMOTE_USER'];

$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];
$passlen = strlen($pass);

if($passlen < 6){
  print "Password Not Long Enough.";
  exit();
}

if($pass != $pass2){
  print "Passwords do not match.";
  exit();
}

$hash = crypt($pass, base64_encode($pass));

$current = file_get_contents($file);

//find and remove old entry
$array = explode("\n",$current);
foreach($array as $arr) {
    if(!(preg_match("/^$login/s",$arr))) {
        $output[] = $arr;
    }
}

$current = implode("\n",$output);
$current .= "\n$login:$hash";
file_put_contents($file, $current);

print "Success"


?>