View Single Post
Old 07-02-2007, 09:50 AM  
takaya
Confirmed User
 
Join Date: Mar 2003
Posts: 50
A simple version

<form action=<?=$PHP_SELF?> method=post>
Username <input name=username><p>
Password <input name=password></p>
| Add <input type=radio name=do_what value=Add> | Modify <input type=radio name=do_what value=Modify> | Delete <input type=radio name=do_what value=Delete> |<br>
<input type=submit value=submit>
</form>
<?
// find path and filename in .htaccess
// change access permission of filename to to allow write
$htaccess_file = "path_of_file/filename";
$username = trim($_POST["username"]); // from yout post form
$password = trim($_POST["password"]); // from your post form
$action = $_POST["do_what"]; // from your post form
$user_string = $username . ":" . crypt($password) ."\n";

if($action == "Add") {
$fp=fopen($htaccess_file, "a+");
if ($fp) {
while (!feof($fp)) {
$htpassinfo = fgets($fp, 4096);
list($ht_username, $ht_password) = explode(":", $htpassinfo);
if(trim($username) == trim($ht_username)) {
echo "Username $username is already existed";
exit;
}
}
if (fwrite($fp, $user_string) === FALSE) {
echo "FAIL to Add";
fclose($fp);
exit;
}
fclose($fp);
echo "Add Successful";
}
} else {
$fp=fopen($htaccess_file, "r");
if ($fp) {
$i=0;
while (!feof($fp)) {
$htpassinfo = fgets($fp, 4096);
list($ht_username, $ht_password) = explode(":", $htpassinfo);
if(trim($username) == trim($ht_username)) {
if($action == "Delete") {
$htlist[$i] = "";
$msg = "Deleted";
} else if($action == "Modify") {
$htlist[$i] = $user_string;
$msg = "Modified";
} else {
$htlist[$i] = $htpassinfo;
}
} else {
$htlist[$i] = $htpassinfo;
}
$i++;
}
fclose($fp);
} else {
echo "fail to open file";
exit;
}
$fp=fopen($htaccess_file, "w");
if ($fp) {
for($i=0;$i<count($htlist);$i++) {
fputs($fp, $htlist[$i]);
}
echo $msg;
}
}
?>
takaya is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote