I have a similar system setup on my site at the moment
<?
//------------------------------------------------------------------------------
// Author: S. Kallemein
// 2002 Copyright(c) Mobile Bridges
//------------------------------------------------------------------------------
// Add a username/password the password file or
// Delete username(s) from the password
//------------------------------------------------------------------------------
// Parameters:
// $username : username
// $passwd : password
//------------------------------------------------------------------------------
// returns:
// OK - Succesfully added/deleted the username/password
// FAILED - insert into the table failed.
//------------------------------------------------------------------------------
header ("Pragma: no-cache");
header ("Expires: -1");
$passfile ='/<path_to_passwordfile>/.htpasswd';
//------------------------------------------------------------------------------
// Author: S. Kallemein
// 2002 Copyright(c) Mobile Bridges
//------------------------------------------------------------------------------
// Delete username(s) from the password
//------------------------------------------------------------------------------
// Parameters:
// $user : username
//------------------------------------------------------------------------------
// returns:
// OK - Succesfully deleted the username/password
// FAILED - access to passfile denied
//------------------------------------------------------------------------------
function DeleteUser($u)
{
global $passfile;
$allusers=file($passfile);
$done=0;
$newusers='';
foreach($allusers as $line)
{
$line=trim($line);
list($user,$pass)=split(':',$line);
if($user!='' && strcmp($u,$user)!=0)
{
$newusers.="$line\n";
}
else $done++;
}
if($done)
{
if($fpass=fopen($passfile,'w'))
{
flock($fpass,2);
fputs($fpass,$newusers);
ftruncate($fpass,ftell($fpass));
fclose($fpass);
echo "OK";
}
else echo "FAILED, ACCESS TO PASSFILE DENIED";
}
}
if(($username=='' || $username=='?') && $passwd=='') {
echo "FAILED";
exit();
}
DeleteUser($username);
if($p!='')
{
// Add user
if($h=fopen($passfile,'a'))
{
$pw=crypt($passwd);
fputs($h,"$username:$pw\n");
fclose($h);
echo "OK";
}
else echo "FAILED, ACCESS TO PASSFILE DENIED";
}
?>
In my .htaccess file I stop any access to my directory appart from the ip address of the company providing the service. I had no problems with the way this is done and quite a few other sites do not as well.
Please provide feedback im unsure though how to access the file above from my server?