Page 1 of 1
Add username and password to .htaccess file
Posted: Fri Jan 03, 2003 9:10 am
by Exelsia
I'm looking for some code to automatically add a username + password to a .htaccess file when someone subscribes for a members section, but this code can't be found anywhere.
Can anyone help me?
Greetings and best wishes,
Niels
Posted: Sat Jan 04, 2003 1:08 am
by volka
Posted: Sat Jan 04, 2003 1:32 am
by Exelsia
Currently I've got this code, but how can I automatticaly add this code to a subscruiption form and thus automatically add username and password to the .thaccess and .thpasswd ?
Code: Select all
<?
if (($user) && ($passwort))
{
# get url
$url = $DOCUMENT_ROOT . dirname($PHP_SELF) . "/.htpasswd";
# make .htaccess and .htpasswd
$htaccess_txt = "AuthType Basic" . "\n";
$htaccess_txt .= "AuthName "protected area"" . "\n";
$htaccess_txt .= "AuthUserFile $url" . "\n";
$htaccess_txt .= "require valid-user" . "\n";
$htpasswd_txt .= "$user:".crypt($passwort,CRYPT_STD_DES)."\n";
# save files
$htaccess= fopen(".htaccess", "w");
$htpasswd= fopen(".htpasswd", "w");
fputs($htaccess, $htaccess_txt);
fputs($htpasswd, $htpasswd_txt);
fclose($htaccess);
fclose($htpasswd);
# output
die ("OK!<HR>" . nl2br($htaccess_txt) . "<HR>" . nl2br($htpasswd_txt));
}
?>
<HTML><HEAD><TITLE> MAKE .htaccess + .htpasswd </TITLE></HEAD>
<BODY>
<FORM METHOD="POST" ACTION="<? echo $PHP_SELF; ?>">
<p>Username: <INPUT TYPE="TEXT" NAME="user"></p>
<p>Passwort: <INPUT TYPE="TEXT" NAME="passwort"></p>
<p><INPUT TYPE="submit" VALUE="make"></p>
</FORM>
</BODY></HTML>
Posted: Sat Jan 04, 2003 5:00 am
by Elmseeker
Check if the form has been submitted like:
Code: Select all
<?
if ( $submit == "make" ) { // form has been submitted...carry on...
if (($user) && ($passwort))
{
# get url
$url = $DOCUMENT_ROOT . dirname($PHP_SELF) . "/.htpasswd";
# make .htaccess and .htpasswd
$htaccess_txt = "AuthType Basic" . "\n";
$htaccess_txt .= "AuthName "protected area"" . "\n";
$htaccess_txt .= "AuthUserFile $url" . "\n";
$htaccess_txt .= "require valid-user" . "\n";
$htpasswd_txt .= "$user:".crypt($passwort,CRYPT_STD_DES)."\n";
# save files
$htaccess= fopen(".htaccess", "w");
$htpasswd= fopen(".htpasswd", "w");
fputs($htaccess, $htaccess_txt);
fputs($htpasswd, $htpasswd_txt);
fclose($htaccess);
fclose($htpasswd);
# output
die ("OK!<HR>" . nl2br($htaccess_txt) . "<HR>" . nl2br($htpasswd_txt));
}
} // close the form submission check...
?>
Posted: Sat Jan 04, 2003 1:40 pm
by oldtimer
As I have my own web server I am not limited to what I can do. I use the system() command to update the .htpasswd file. I created a nice interface for it as well that my friends can add users to their protected folders and add new folders. If you do not have access like I have then the previous post works great. You can also make it read out to your screen so you know who is in there.