Question

quick question. I setup a timecard and PO system for a family business. The employees enter their username and password to enter the system. Every time the family hired someone though they send me the info. I use htpasswd generator, and open the file up, add it, and then re-upload use ftp. My wife enter's their info into the db using a php page i setup though and i'm wondering if there is a way to allow her a txt field that can she can copy and past the generated pw into the htpasswd file. Without having me to always change it.

To summarize: is there a form command that when i put in the txt field and push submit it automatically puts the txt into the htpasswd file

Was it helpful?

Solution

You'll need to create a php page. This answer has an example of the code you need:

$username = $_POST['user'];
$password = $_POST['pass'];
$new_username = $_POST['newuser'];
$new_password = $_POST['newpass'];
$action = $_POST['action'];
//read the file into an array
$lines = explode("\n", file_get_contents('.htpasswd'));

//read the array and change the data if found
$new_file = "";
foreach($lines as $line)
{
    $line = preg_replace('/\s+/','',$line); // remove spaces
    if ($line) {
        list($user, $pass) = split(":", $line, 2);
        if ($user == $username) {
            if ($action == "password") {
                $new_file .= $user.':'.$new_password."\n";
            } else {
                $new_file .= $new_username.':'.$pass."\n";
            }
        } else {
            $new_file .= $user.':'.$pass."\n";
        }
    }
}

//save the information
$f=fopen(".htpasswd","w") or die("couldn't open the file");
fwrite($f,$new_file);
fclose($f);

Here is also a slightly more complete solution. Or just look it up on Google.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top