View Single Post
Old 10-19-2006, 02:18 PM   · #4
maximum
MyDomains.net
 
maximum's Avatar
 
Name: -Unknown-
Location: Inside your head...
Trader Rating: (15)
Join Date: Oct 2005
Posts: 804
NP$: 42.70 (Donate)
maximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud ofmaximum has much to be proud of
Child Abuse Special Olympics Save a Life Baby Health Autism
This isn't truelly, in iteslf "automatically", as you put it. But, you said you know how to write to the .htpasswd file (and this script assumes you would be doing so with PHP ). This shows a form to be filled-out (request for username and password), and auto-adds it to .htpasswd file after it encrypts the password (.htaccess usernames are not encrypted, only passwords are). You can alter it to get the variables passed by means other than the form, should you need to do so. This script only adds new entries, and does not let you alter current ones. If you need that added functionality, just let me know

".htaccess" file:
Code:
AuthUserFile /path/to/your/hidden/password/files/.htpasswd AuthName "Private Directory" AuthType Basic require valid-user

CreateUser.php:
PHP Code:
<?
$UserName
= $_POST['user'];
$ClearPassword = $_POST['pass'];
if(
$UserName != "" && $ClearPassword != "") {
$EncryptedPassword = crypt($ClearPassword, base64_encode($ClearPassword));
$PasswordFile = '/path/to/your/hidden/password/files/.htpasswd';
$Login = "$UserName:$EncryptedPassword\n";
$handle = fopen($PasswordFile, 'a');
fwrite($handle, $Login);
fclose($handle);
die(
"<b>Login added to<br><font color=red>".$PasswordFile."</font></b><br><br>UserName: <font color=red>".$UserName."</font><br>Password: <font color=red>".$ClearPassword."</font><br>Encrypted As: <font color=red>".$EncryptedPassword."</font><br><br>Add <a href=".$PHP_SELF.">another</a> user."); }
?>
<center><table>
<form action="<? $PHP_SELF; ?>" method="post">
<table>
<tr><td>UserName: </td><td><input type="text" name="user"></td></tr>
<tr><td>Password: </td><td><input type="text" name="pass"></td></tr>
<tr><td colspan=2 align=center><input type=submit value="Add User"></td></tr>
</form></table></center>


A few (just incase you didn't know) security tips:
1) Move your .htpasswd file to a sub-root folder (ie: one that is not directly web accessable).
-OR-
2) Rename your .htpasswd file to something other than ".htpasswd"...As long as you put the name correctly in the .htaccess file itself, it will read it. Name it something odd and hard to guess, and create an odd extention (example: ValidMembers.list - not very creative, think of your own...just an example ). Instead of your .htaccess having the obvious:
AuthUserFile /path/to/your/protected/area/.htpasswd
it would have:
AuthUserFile /path/to/your/protected/area/ValidMembers.list

Need any more help, or don't understand anything there...just give me a yell here or in PM

Disclaimer: I acknowledge that there are more effective and clean ways to do this. But, I am not trying to write the above code for commercial use. Those finding errors, please let me know. Those wishing to educate me on my PHP coding, are also gladly asked to do so. However, those wishing to just nag or make themselves look smarter.....Go code something yourself...Preferably with PHP's ever-so-useful die() command
------------------------------------------------------------------
Update to my would-make-SecondVersion-think-I'm-stupid code:

The following allows you to edit the file directly Not the best code in the world, but think it will do what you are seeking, if you wish to ad users by hand. To add them via another script, remove the form elements and just create a script to post to this.

PHP Code:
<?
$HtpassFile
= "/path/to/your/hidden/password/files/.htpasswd";
$Contents = htmlentities(implode("", file ($HtpassFile)));
if(
$edit != ""){
$editfile=fopen($HtpassFile, "w+");
fputs($editfile, $edit);
fclose($editfile);
}
?>
<form action="<? echo $PHP_SELF; ?>" method="post">
<textarea name="edit" cols="45" rows="20">
<?
echo $Contents;
?>
</textarea>
<br>
<input type=submit value="Edit List">
&nbsp;&nbsp;&nbsp;
<input type=button value="Refresh List" onclick="javascript:window.location.replace('<? echo $PHP_SELF; ?>')"></form><br><br>
<?
$DidAdd
="";
$UserName = $_POST['user'];
$ClearPassword = $_POST['pass'];
if(
$UserName != "" && $ClearPassword != "") {
$EncryptedPassword = crypt($ClearPassword, base64_encode($ClearPassword));
$Login = "$UserName:$EncryptedPassword\n";
$handle = fopen($HtpassFile, 'a');
fwrite($handle, $Login);
fclose($handle);
$DidAdd ="<b>Login added to<br><font color=red>".$HtpassFile."</font></b><br><br>UserName: <font color=red>".$UserName."</font><br>Password: <font color=red>".$ClearPassword."</font><br>Encrypted As: <font color=red>".$EncryptedPassword."</font><br><br>"; }
?>
<table>
<form action="<? $PHP_SELF; ?>" method="post">
<table>
<tr><td>UserName: </td><td><input type="text" name="user"></td></tr>
<tr><td>Password: </td><td><input type="text" name="pass"></td></tr>
<tr><td colspan=2 align=center><input type=submit value="Add User"></td></tr>
</form></table><br><br>
<? echo $DidAdd; ?>


NOTE: When editing, make sure each username/password combo is on a new line. It may not end-up that way, if you edit the file in the text-box, so "Refresh List" to make sure it is added on new line for each user.


Please register or log-in into NamePros to hide ads
__________________
Support the Children's Miracle Network hospitals
■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□
Domains For Sale list (many multi-TLD and/or related-term package-deals): MyDomains.net
maximum is offline  
  Reply With Quote
Site Sponsors
Exdon http://www.mobisitetrader.com/ YUPPADS
Advertise your business at NamePros
All times are GMT -7. The time now is 04:51 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.