Add username and password to .htaccess file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Exelsia
Forum Newbie
Posts: 3
Joined: Fri Jan 03, 2003 9:10 am
Location: Antwerp, Belgium
Contact:

Add username and password to .htaccess file

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Exelsia
Forum Newbie
Posts: 3
Joined: Fri Jan 03, 2003 9:10 am
Location: Antwerp, Belgium
Contact:

Post 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))
&#123;
	# 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));

&#125;      

?>
<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>
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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...
?>
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post 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.
Post Reply