Adding, removing, and changing user names and passwords in .htpasswd with PHP Here is some code I wrote to add, remove, and change user names and passwords in .htpasswd with PHP:
function adduser($user, $pass) {

    try {

        $htpasswd = '.htpasswd';

        //$hash = '{SHA}'.crypt($pass, base64_encode($pass));

        $hash = crypt_apr1_md5($pass); //APR1-MD5

        $contents = $user . ':' . $hash;

        $lines = explode(PHP_EOL, file_get_contents($htpasswd)); // get .htpasswd

        print('&lt;h4&gt;input:&lt;/h4&gt;&lt;pre&gt;'.print_r(implode(PHP_EOL, $lines),true).'&lt;/pre&gt;');

        $exists = false;

        foreach($lines as $line){

            $existing_user = explode( ':', $line );

            if ($existing_user[0] == $user) { //checks if user exists

                $contents = str_replace($line, $contents, $lines); //changes password for user

                $contents = implode(PHP_EOL, $contents);

                $exists = true;

                if ($pass == '') { // removes user if password is empty

                    $contents = str_replace($line, '', $lines); //removes user

                    $contents = array_filter($contents); // cleans empty space in array

                    $contents = implode(PHP_EOL, $contents);

                    $exists = true;

                }

            }

        }


        if ($exists == false) {

            $contents = implode(PHP_EOL, $lines) . PHP_EOL . $contents;
        }

        file_put_contents($htpasswd, $contents);

        print('&lt;h4&gt;output:&lt;/h4&gt;&lt;pre&gt;'.print_r($contents,true).'&lt;/pre&gt;');

    }catch(Exception $e) {

        echo '&lt;h3&gt;fail: &lt;/h3&gt;' . $e-&gt;getMessage();
    
    }

}

if(isset($_GET['user'])){

    adduser($_GET['user'], $_GET['pass']);

    echo '&lt;h3&gt;success&lt;/h3&gt;';

}else{

    $htpasswd = '.htpasswd';

    $lines = explode(PHP_EOL, file_get_contents($htpasswd)); // get .htpasswd

    print('&lt;h4&gt;.htpasswd:&lt;/h4&gt;&lt;pre&gt;'.print_r(implode(PHP_EOL, $lines),true).'&lt;/pre&gt;');

    echo '&lt;h3&gt;no user set&lt;/h3&gt;';
}

I would love to streamline it and clean it up.
Here is my version based on two personal preferences: I hate scroll bars and I hate repititions. Hence I prefer everything typed once and also I like my code being fully visible in the default code area on Stack Overflow.

for this reason I removed that double spacing which just hurts my eyes
also I removed the try catch which is a cargo cult code that makes no sense. I was never able to understand what's the point in writing a try catch that's the only job is to echo the error message when without a try catch PHP will do exactly the same - echo the error message
also I removed the code repetitions such as mentioning the filename in a dozen places
also I changed the algorithm, to make it add a user line only once
I also removed that uncertainty when we can't make our mind whether we are working with an array or with a text
I also changed the function name as it doesn't only add a user
and some other improvements such as following the HTTP guidelines and the ability to choose the hashing algorithm without using inline comments

here it goes
&lt;?php
$filename = &quot;.htpasswd&quot;;
if (isset($_POST['user'])) {
    manage_htpasswd($_POST['user'], $_POST['pass'], $filename);
    header(&quot;Location: &quot;.$_SERVER['REQUEST_URI']);
    exit;
}
function manage_htpasswd($user, $pass, $filename, $algo = 'crypt_apr1_md5')
{
    $lines = file($filename);
    foreach ($lines as $i =&gt; $line) {
        $existing_user = explode(':', $line);
        if ($existing_user[0] === $user) {
            unset($lines[$i]);
            break;
        }
    }
    if ($pass) {
        $lines[] = &quot;$user:&quot; . $algo($pass) . PHP_EOL;
    }
    file_put_contents($filename, $lines);
}
?&gt;
&lt;form method=&quot;post&quot;&gt;
    User: &lt;input type=&quot;text&quot; name=&quot;user&quot;&gt;&lt;br&gt;
    Pass:&lt;input type=&quot;text&quot; name=&quot;pass&quot;&gt;&lt;br&gt;
    &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;
&lt;h4&gt;&lt;?= $filename ?&gt;:&lt;/h4&gt;
&lt;pre&gt;
&lt;?= file_get_contents($filename) ?&gt;
&lt;/pre&gt;